Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/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 数字与用户输入的乘积之和_Python_Python 3.x - Fatal编程技术网

Python 数字与用户输入的乘积之和

Python 数字与用户输入的乘积之和,python,python-3.x,Python,Python 3.x,我如何使这个程序工作 问题 我需要设置用户可以输入多少个浮点输入。然后将每个输入乘以一个数字,然后对每个乘积求和 代码 userInput = int(input("Enter how many numbers you would like to input? ")) numList = [None] * userInput for x in range(userInput): numList[x] = float(input("What is the value of number 1

我如何使这个程序工作

问题

我需要设置用户可以输入多少个浮点输入。然后将每个输入乘以一个数字,然后对每个乘积求和

代码

userInput = int(input("Enter how many numbers you would like to input? "))
numList = [None] * userInput
for x in range(userInput):
    numList[x] = float(input("What is the value of number 1? "))
multiplicand = int(input("Enter the multiplicand: "))
for y in numList:
product = multiplicand * y
sumOfproduct = sum(product)
print(sumOfproduct)
输出应如下所示:

输入要输入的数字数?三,

数字1的值是多少?二,

数字2的值是多少?三,

数字3的值是多少?一,

输入被乘数:5


总值为:30

您可以这样做:

userInput = int(input("Enter how many numbers you would like to input? "))
multiplicand = int(input("Enter the multiplicand: "))
ans = 0
for x in range(userInput):
    num = float(input("What is the value of number " + str(x) + " ? "))
    ans += num*multiplicand

print(ans)

这将解决您的问题: `


`

请不要诋毁你的问题。这里的问题旨在帮助未来的访问者解决同样的问题,而不仅仅是你。
userInput = int(input("Enter how many numbers you would like to input? "))
numList = [None] * userInput
for x in range(userInput):
    numList[x] = float(input("What is the value of number "+str(x+1)+"?"))
multiplicand = int(input("Enter the multiplicand: "))
l = sum(numList)*multiplicand
print (l)
temp1 = 1
temp2 = 0
user_input=[]
no_of_input=int(input("Enter how many numbers you would like to input? "))

for i in range(0,no_of_input) :
    num=float(input("enter input {0}".format(i+1)))
    user_input.append(num)

multiplicand=float(input(("enter the multiplicand")))



for j in range(0,no_of_input) :
    temp1=multiplicand * user_input[j]
    temp2= temp2 + temp1



print("total value is {0}".format(temp2))