如何找到嵌套循环中数字列表的和?
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...
我需要找到如何计算每个循环的不定数之和,我现在遇到的问题是循环没有为每个人重置每个值,我得到一个总和。
你的问题措词很差,但如果我理解正确,这可能行得通。
假设您使用的是Python 3。对于Python 2.7,将
input()
替换为raw_input()
希望这能回答你的问题
这是我能想到的最简单的解决方案:
输出:
其中
abc
包含:我想如果你想记录所有的内环结果而不打印,你需要一个列表。我已经测试了你的输入,Python2.7可以。
相关问题 更多 >
编程相关推荐