Chr*_*ris 1 python list while-loop pyscripter
我有一个数字列表,我想要这样,在输入3个数字之后,它们将总结,除非其中一个数字是13,否则其余数字将无效并且不能总结.
因此,如果列表是[1, 13, 2]
最终总和将是1.
total = 0
def lucky_sum():
total = 0
lucky_sum = []
a = input("Input a number")
b = input("Input a number")
c = input("Input a number")
lucky_sum.append(a)
lucky_sum.append(b)
lucky_sum.append(c)
while **What do I place here??** != 13:
total += **and here**
if i== 13:
break
print total
lucky_sum()
Run Code Online (Sandbox Code Playgroud)
你可以不使用while循环来做到这一点:
for i in lucky_sum():
if i == 13:
break
total += i
Run Code Online (Sandbox Code Playgroud)
编辑:正如评论中所建议的那样,在最后lucky_sum
添加一个return语句.