【问题标题】:How to find the sum of an entered list of numbers如何找到输入的数字列表的总和
【发布时间】:2019-10-24 06:45:03
【问题描述】:

我对 python 还是很陌生,并且正在完成一项学校作业。该程序应该是一个循环,允许用户输入一系列数字,只要输入的数字为 0 或更大,这些数字就会继续运行。输入负数后,程序应创建所有输入数字的总和。非常感谢您的帮助,谢谢!

#This program calculates the sum of multiple numbers

#Initialize accumulator
total = 0

#Calculate the sum of numbers
while keep_going >= 0:
    keep_going = int(input('Enter a number: '))
    total += keep_going
print(total)

【问题讨论】:

    标签: python python-3.7


    【解决方案1】:

    欢迎来到 StackOverflow Christian,欢迎进入伟大的编程世界 =)

    关于您的代码的几点说明:

    • keep_going = >-0 毫无意义。 > 是一个比较运算符,你必须用它来比较两个表达式,例如var1 > var2,它会返回一个布尔值。
    • while keep_going == 0: 是一个不错的开始,但不会做你想做的事。如果输入的数字大于或等于零,则循环必须继续进行,而不仅仅是keep_going 等于零。将== 更改为>=
    • int(input('Enter a number: ')) 是要走的路,但你为什么用了两次呢?附带说明一下,您只是第二次将输入数字存储在变量中。
    • 最后,您需要在循环中实际使用total 来存储用户输入。

    祝你好运!

    PS:虽然 stackoverflow 非常适合快速获得解决方案,但我真的建议您真正了解您的代码错误的原因,以及提供的解决方案为何有效。它将极大地帮助您成为一名优秀的程序员;)

    【讨论】:

      【解决方案2】:
      #Calculate the sum of numbers
      saved_numbers = [] # A list of numbers to fill up. 
      while True: # A way to write 'keep doing this until I stop ('break' in this case...)'
          number = int(input('Enter a number: '))
          if number < 0: # If the number is negative
              break # Exits the current while loop for us. 
          else: # Otherwise, save the number.
              saved_numbers.append(number) 
      
      sum = sum(saved_numbers) # Getting the sum of the numbers the user entered!
      print(sum)
      

      【讨论】:

      • 这个答案在没有 cmets 的情况下会更好,IMO。
      • 感谢反馈...在考虑到 OP 是新的并且需要一些细节后添加它们
      • 我知道你的意思,但我认为普通文本更适合大多数此类解释。只是我个人认为最好在答案中建模良好的编码实践,尤其是对于初学者。
      【解决方案3】:

      你可以让它更简单。您不需要 keep_going 变量。只需使用 total 变量,如果输入的数字为 0 或大于 0,则添加到变量中。如果数字小于 0,则退出 while 循环:

      #Initialize accumulator
      total = 0
      
      #Calculate the sum of numbers
      while(True):
          num = int(input('Enter a number: '))
          if num < 0:
              break
          else:
              total = total + num
      
      print(total)
      

      【讨论】:

        【解决方案4】:

        这也可以使用recursion 而不是while 来完成。即:

        def count_total(total=0):
            keep_going = int(input('Enter a number: '))
            if keep_going >= 0:
                total += keep_going
                count_total(total)
            else:
                print('Total : %d' % total)
        
        
        count_total()
        

        【讨论】:

        • 虽然我真的很喜欢递归,但把它介绍给一个新程序员是邪恶的边缘......这是你的+1:'D
        • 程序员,任何程序员,真的会这样做吗?没有。
        • @NiziL 至少它是边界线:'D
        【解决方案5】:

        你可以试试这个解决方案:

        def func():
            i = 0
            while i >= 0:
                i = int(input('Enter a number: '))
                yield (i >= 0) * i
        
        print(sum(func()))
        

        请记住,在 Python 中,True 等于 1False 等于 0

        【讨论】:

          【解决方案6】:

          只需跟踪输入的数字,稍后使用 python in-built sum 函数计算总和。

          keep_going = int(input('Enter a number: '))
          entered_nums = []
          while keep_going >= 0:
              entered_nums.append(keep_going)
              keep_going = int(input('Enter a number: '))
          
          print('Entered numbers : ', entered_nums)
          print('Entered numbers sum : ', sum(entered_nums))
          

          【讨论】:

            猜你喜欢
            • 2019-10-30
            • 2013-03-31
            • 2017-03-17
            • 2021-11-20
            • 1970-01-01
            • 2023-02-04
            • 1970-01-01
            • 2014-12-07
            • 1970-01-01
            相关资源
            最近更新 更多