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

时间:2016-02-29 05:28:33

标签: 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? "))
multiplicand = int(input("Enter the multiplicand: "))
for y in numList:
product = multiplicand * y
sumOfproduct = sum(product)
print(sumOfproduct)

输出应如下所示:

输入您想输入的号码数量? 3

1号的价值是多少? 2

2号的价值是多少? 3

3号的价值是多少? 1

输入被乘数:5

总值为:30

3 个答案:

答案 0 :(得分: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 "+str(x+1)+"?"))
multiplicand = int(input("Enter the multiplicand: "))
l = sum(numList)*multiplicand
print (l)

答案 1 :(得分:0)

你可以这样做:

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)

答案 2 :(得分:0)

这应该解决你的问题: `

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))

`