【问题标题】:How to deal with that loop?如何处理那个循环?
【发布时间】:2021-03-20 12:53:59
【问题描述】:

尝试为循环编写代码,该循环从输入中获取数字,直到输入为“The End”。在那一刻,循环应该停止并返回在“The End”之前输入的所有数字的总和。我有一种直观的感觉,它应该非常容易执行,但是我被该代码所困扰。需要冲动。我是这么想的,但是当我尝试将输入转换为整数以便对它们求和时,当我最终输入“The End”时它会返回错误,因为它在逻辑上不能成为整数。

n = 0
summa = 0
while n != "The End":
    n = input()
    summa += int(n)

print(summa)

【问题讨论】:

  • 哪种语言?指定语言,以便合适的人可以联系到您。

标签: loops for-loop while-loop


【解决方案1】:
while n != "The End":
    n = input()
    # At this part, add check if n is integer, update new summa, otherwise no action
    summa += int(n)

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    相关资源
    最近更新 更多