【发布时间】:2020-01-26 22:20:44
【问题描述】:
我在 Python 中有以下代码:
如果我按原样运行代码,如果我输入数字 9,它将运行从 0 到 8 的数字列表。并且每次我重新运行代码时,它都会自动从 0 开始。附加代码做什么我需要让它从上一个循环的最终结果开始运行(即我从 9 开始,然后当我第二次使用 8 时,它从 8 开始运行循环,因为这是上一个循环的最终结果) ?
while user_play == "y":
# Ask the user how many numbers to loop through
ask_user = input("How many numbers would you like to loop? ")
# Loop through the numbers. (Be sure to cast the string into an integer.)
for number in range(0,int(ask_user)):
# Print each number in the range
print(number)
# Once complete, ask the user if they would like to continue
user_play = input("Would you like to continue? ")```
【问题讨论】:
-
分配一个变量作为
range()的开头,然后在每次运行时更新它
标签: python for-loop input while-loop range