【问题标题】:Receiving integers from the user until they enter 0 [closed]从用户那里接收整数,直到他们输入 0 [关闭]
【发布时间】:2019-11-16 23:09:47
【问题描述】:

这是我使用 Python 编程的第二周,之前从未编写过任何东西。一步一步欣赏。

我不知道从哪里开始。

【问题讨论】:

  • 到目前为止你得到了什么?你知道怎么读一个数字吗?你知道如何读取多个数字吗?你知道循环吗?向我们展示您拥有的任何代码。
  • 可能从接收用户的整数开始(提示:这个操作包括两个步骤)。

标签: python python-3.x


【解决方案1】:
try:
    count = 0
    while True:
        user = int(input('Insert Number: '))
        count += user
        if user == 0:
            break
    print(count)
except ValueError:
    print('Please Insert Numbers Only!')

【讨论】:

    【解决方案2】:

    首先,使用 while 循环作为输入。除非您需要进一步的帮助,否则我会将总结部分留给您:

    cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
    while cc != 0:
        cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
    

    【讨论】:

      【解决方案3】:
      def is_int(num):
          try:
              return int(num)
          except:
              return False
      
      total = 0
      while True:
          in_user = is_int(input("input integer: "))
          if in_user is not False:
              total += in_user
          if in_user is 0:
              break
      
      print(total)
      

      【讨论】:

        猜你喜欢
        • 2021-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-29
        • 1970-01-01
        • 2013-01-18
        相关资源
        最近更新 更多