【问题标题】:Python: Calculating a sum for each loopPython:计算每个循环的总和
【发布时间】:2013-03-23 17:16:36
【问题描述】:

如何找到嵌套循环中的数字列表的总和?

    s=0
    people=eval(input())
    for i in range(people):
        firstn=input()
        lastn=input()
        numbers=(eval(input()))

        print(firstn, lastn, numbers)
        for b in range(numbers):

        numbers=eval(input())
        s+=numbers

        print(b)

输入如下:

    5 #nubmer of people I need to calculate
    Jane #firstname
    Doe #lastname
    4 #number of floats for each person, pretty sure this is for the second loop
    38.4 #these are the floats that i need to calculate for each person to find their sum
    29.3
    33.3
    109.74
    William #loop should reset here as this is the next person's first name
    Jones
    2
    88.8
    99.9
    firstname
    lastname
    number of floats
    float1
    float2...

我需要找到如何计算每个循环的不定数的总和,我现在遇到的问题是循环没有为每个人重置每个值,我得到了一个总和。

【问题讨论】:

  • 为什么是eval?使用int()float()
  • TypeError: 'float' 对象不能被解释为整数
  • 尝试类似:numbers=float(input())numbers=int(input())
  • @James 如果用户输入__import__('os').system("echo 'Random code execution'") 怎么办? 从不使用eval。如果您想评估文字,请使用ast.literal_eval,这是安全的。
  • 非常好的观点。所有开发者都需要将这幅漫画打印出来并张贴在他们的办公室:xkcd.com/327

标签: python loops nested sum


【解决方案1】:
s = []
people = int(raw_input())
for i in range(people):
    firstn = raw_input()
    lastn = raw_input()
    numbers = int(raw_input())

    print(firstn, lastn, numbers)
    temp = 0
    for b in range(numbers):
        numbers = float(raw_input())
        temp += numbers
    s.append(temp)
print(s)

我认为,如果您想记录内部循环的所有结果并且不打印,则需要一个列表。我已经测试了你给定的输入,Python2.7 没问题。

【讨论】:

  • 我已经编辑了我的答案并测试了代码。我使用 Python2.7 为您的输入工作。你可以试一试。 @詹姆斯
  • 是python 3,说raw_input没有定义
  • 只需将所有raw_input() 替换为input() 就可以了。@James
  • 它确实有效,非常感谢,有什么方法可以让每个人的名字旁边的每个总数,否则在第一个循环中打印出来?
  • 温度是每个人的总和。你可以在上面做任何你想做的事情。但我认为您可能想要使用它们,所以我存储它而不是打印它。如果您愿意,只需打印它们,将 s.append(temp) 替换为 print(temp).@James
【解决方案2】:

这是我能想到的最简单的解决方案:

nop=int(input())
for _ in range(nop):
    fname,lname=input(),input()
    n=int(input())
    summ=sum(float(input()) for _ in range(n))
    print("For {0} {1} the sum is {2}".format(fname,lname,summ))

输出:

$ python3 foo.py < abc
For Jane Doe the sum is 210.74
For William Jones the sum is 188.7

其中abc 包含:

2
Jane
Doe
4
38.4
29.3
33.3
109.74
William
Jones
2
88.8
99.9

【讨论】:

    【解决方案3】:

    您的问题措辞不佳,但如果我理解正确,这可能会奏效。

    people = int(input('Enter number of people: ')) # eval is generally not a good idea
    for i in range(people):
        firstn = input()
        lastn = input()
    
        numbers= int(input('Enter number: '))
    
        print(firstn, lastn, numbers)
    
        print(sum(numbers)) # prints sum of 0,1,2...numbers-1
    

    这是假设您使用的是 Python 3。对于 Python 2.7,请将 input() 替换为 raw_input()

    希望这能回答你的问题

    【讨论】:

    • 这是我的第二个问题,我似乎无法将输入作为我想要的列表,无论如何它们都没有显示我如何缩进它们,无限数量的输入都是浮动。
    • 类似:input_list = [] while True: in = float(input('Enter next input (enter nothing to cancel)')) if not in: break else: input_list.append(in)
    • @James 最好在问题正文中提供示例输入/输出。
    • 我得到的结果是名字,姓氏,每个浮点输入的总和,而不是我需要的浮点数。
    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    相关资源
    最近更新 更多