Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从for循环中的用户输入求和?_Python - Fatal编程技术网

Python 如何从for循环中的用户输入求和?

Python 如何从for循环中的用户输入求和?,python,Python,分配给我的一个练习要求我(通过输入功能)询问价格,并重复输入7次。然后,我需要总结用户输入的数字,我有点困惑我应该朝哪个方向走 for in range(7): input("What is the price? " 你在正确的轨道上。将总数存储在定义的变量中,并从0开始。它看起来像这样: total = 0 for i in range(7): total += float(input("What is the price? "))

分配给我的一个练习要求我(通过输入功能)询问价格,并重复输入7次。然后,我需要总结用户输入的数字,我有点困惑我应该朝哪个方向走

    for in range(7):
        input("What is the price? " 

你在正确的轨道上。将总数存储在定义的变量中,并从0开始。它看起来像这样:

    total = 0
    for i in range(7):
        total += float(input("What is the price? "))
每次我们得到输入时,它的值都会加到总数中。请注意,如果您知道数字是整数,可以使用int而不是float。

您可以这样做:

total = 0
for i in range(7):
    total += int(input("What is the price? "))
#create empty list called numbers

numbers = []

#create for loop to take user input and append to list

for i in range(7):
    numbers.append(input('What is the Price?'))

#then convert the list into integer type and call the sum function on it

sum(int(i) for i in numbers)

你的缩进有点小,只是说。@ChristopherFernandes如果回答者的帖子解决了你的问题,请接受和/或根据陈述投票。