【发布时间】:2023-03-25 02:33:01
【问题描述】:
我正在使用 python3,并且我有这段代码,我在其中向用户询问三个输入。然后我对它们进行计算。我想继续将计算结果添加到列表中。该怎么做?
...
if choice == '1': #my fist input x
while True:
x = int(input('Please insert the number of things you consider yourself a fan of'))
if x > 0:
break
else:
print('Please insert a number higher than 0')
elif choice == '2': #my second input y
while True:
y = int(input('Please insert the number of hobbies you like to do every month'))
if y % 4 == 0:
break
else:
print('Please insert a valid number')
elif choice == '3': #my third input z
while True:
z = int(input('Please insert the number of sports you like'))
if z > 0:
break
else:
print('Please insert a number higher than 0')
elif choice == '4': #the calculation part
import math
def square_root():
c=(42 * y ** 2)/(z + 1)
nerd_score=int(x*math.sqrt(c))
return nerd_score
print('your nerd score is', square_root())
我希望循环继续进行,并将每个结果添加到列表中。直到用户退出循环。
【问题讨论】:
-
创建一个列表,即
list = [],然后像list.append(result)这样附加你的结果我想这就是你所要求的。 -
如果我这样做,我只会得到一个分数。由于这是一个循环,我希望用户能够添加多组 input(x,y,z) 并且每个结果都将添加到一个列表中。
-
您是否要返回设置选项的主循环并继续收集值直到选择 4?您应该扩展您的代码摘录以包含主循环,以便我们拥有完整的图片
-
我想收集三个数字然后在4处得到结果。然后我想继续将结果添加到列表中。
标签: python list loops while-loop