0

How does range work

I want to out put a leaderboard style answer to 9 with a full stop after each number. I tried to use this and just want to check if their is a more efficient way or does this even work: num = 1 for i in range(1, 9): print(num + ‘.’\n) num += 1

13th Jan 2022, 7:54 PM
Musab
Musab - avatar
4 RĂ©ponses
+ 2
You need to change range to one more and edit print like this print(str(num)+'.\n')
13th Jan 2022, 8:03 PM
Bartek
0
You may as well delete the line num += 1. Each time through the loop, num advances to the next value in the range, overwriting whatever value you might set inside the loop.
13th Jan 2022, 10:12 PM
Brian
Brian - avatar
0
So should I just use i in place of num?
13th Jan 2022, 10:14 PM
Musab
Musab - avatar
0
I need to correct myself. I was thinking num was the loop index, like this for num in range(1, 9): So, yes, you may use the above loop, or use i instead of num in the print statement.
13th Jan 2022, 10:27 PM
Brian
Brian - avatar