问题
How can I simply transform this loop to count up from 1 to 100, and display the numbers? I'm starting to code recently. It works fine when counting down, but I can't figure out how to make it go from 1 -100
example:
count = 100
while count > 0 :
print(count)
count = count - 1
回答1:
Start at 1, and change your conditional to break out when you reach 100. Add 1 each loop through.
count = 1
while count <= 100:
print(count)
count += 1
回答2:
If you use a for loop it gets really easy:
for number in range(1,101):
print(number)
And for going from 100 down to 1:
for number in range(100,0,-1):
print(number)
回答3:
just start your count at 1, change your check statement to check if the number is less than 100, and use "count = count + 1" Should work, good luck!
回答4:
Can try 'reversed':
>>> for i in reversed(range(1,11)):
... print i
...
10
9
8
7
6
5
4
3
2
1
回答5:
Basically just do the opposite of what you've already got.
count = 1
while count < 101:
print(count)
count = count + 1
回答6:
Try this.
count = 0
while count <= 100:
print ("Count = ", count)
count = count + 1
来源:https://stackoverflow.com/questions/33401542/python-count-up-down-loop