【问题标题】:How to receive a num at each step and continue until zero is entered; then this program should print the sum of enter nums如何在每一步接收一个数字并继续直到输入零;那么这个程序应该打印输入数字的总和
【发布时间】:2022-11-30 06:33:02
【问题描述】:

如何编写一个程序,在每一步从输入中接收一个数字并继续工作直到输入零。输入零数字后,该程序应打印输入数字的总和。我想在 n 个不同的行中得到 n 个不同的数字,当它达到零时它会停止

【问题讨论】:

  • 你能举一个你当前代码的例子吗?
  • 欢迎来到计算器。尝试给我们一个minimal example,以便我们知道你做了什么以及问题出在哪里。

标签: python


【解决方案1】:

在这里,我使用了一个 while 循环,它一直持续到用户输入为 0,然后打印出总和。

res = 0
user_input = int(input('Input a number: '))
while user_input != 0:
    user_input=int(input('Input a number: '))
    res+= user_input
print("You entered 0 so the program stopped. The sum of your inputs is: {}".format(res))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-30
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多