pravar
(Pravar S.)
August 13, 2023, 2:53pm
1
#write a program that generates the following output using a for loop.
‘for alpha in range(65,91):
print(chr(alpha),end=’‘)
print()
for alpha in range(122,96,-1): /// doubt
print(chr(alpha),end=’,‘)
print()’
output is A B C D E F G H I J K L M N O P Q R S T U V W X Y Z z, y, x, w, v, u, t, s, r, q, p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a,
QUESTION- When I use for alpha in range(122,97) i don’t get the same output as above? Can anyone PLZ explain why?
e-dreyer
(Erik)
August 13, 2023, 3:18pm
2
range is normally used for increasing values, so the fact that you want to go down, you need to add the -1.
Python basically starts with 122, then checks if it matches the condition, which is false without the -1.
1 Like
What output do you get, how is it different?
1 Like
kknechtel
(Karl Knechtel)
August 13, 2023, 4:03pm
4
If you count upwards (the default for range
), starting at 122, will you reach 97? If you start at 122, what should you add each time, in order to reach 97?
(Also: what happens if you try using help(range)
at the interpreter prompt? Does this help you understand the problem?)
1 Like
pravar
(Pravar S.)
August 14, 2023, 8:42am
6
I get the output as A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
There is no output for the second part.
pravar
(Pravar S.)
August 14, 2023, 8:44am
7
Woah haha! Thanks Karl. I understod now! Thanks for the tip - help(range) Have a good day!