Python Count up & Down loop

走远了吗. 提交于 2019-12-23 04:52:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!