python-3.x 使用环困难

inkz8wg9  于 2023-08-08  发布在  Python
关注(0)|答案(2)|浏览(83)

我理解循环有困难。
这是我的问题。
编写一个表达式,只要用户输入一个非负数,它就执行循环体。

user_num =9;
while '"Your solution goes here"':
   print('body')
   user_num = int(input))

print ('Done.')

字符串
有谁能帮我解释一下循环是如何工作的吗?

b4lqfgs4

b4lqfgs41#

下面是一个解决方案,每行都有一些注解:

user_num =9; #Initialize a random non-native number to be able to go to the core of the while loop

while (user_num >= 0): #After each user input, check if the input is a non-negative number
  #If user input is a non-negative number, you end up here, inside the while loop
  print("Body") #This is one of the statements. It prints "Body"
  user_num = int(input())  #Acquire a new input from the user and store it in the variable user_num. At this point, the loop runs again using the new user input.
#If user input doesn't meet the while loop's condition, you end up here, outside the while loop
print('Done.')

字符串
第一行user_num = 9作为用户的第一个输入。您希望这个数字满足while循环的条件,以便它在其中执行语句
下面是一个很好的开始while循环的地方:https://www.w3schools.com/python/python_while_loops.asp

5m1hhzi4

5m1hhzi42#

这是正确答案:

while user_num >=0:

字符串

相关问题