写一个函数add_up,它添加用户提供的整数,当用户写“停止”时停止。
我收到一条错误消息,指出我的代码未执行所述功能。
x = input("First number: ")
def add_up(x):
Num = 0
while x != "Stop":
Num = int(x) + Num
x = input("Another numer: ")
return Num
print(add_up(x))
答案 0 :(得分:0)
试试这个:
>>> def add_up():
... num = 0
... while True:
... x = input("Write a number: ")
... if x.lower() == 'stop':
... return num
... num += int(x)
...
>>> print(add_up())
Write a number: 1
Write a number: 5
Write a number: 1
Write a number: Stop
7