【发布时间】: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