[英]Sum of the product of a number and user inputs
如何使該程序正常工作?
問題
我需要設置用戶可以輸入多少個浮點輸入。 然后,將每個輸入乘以一個數字,並對每個乘積求和。
碼
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
您可以這樣操作:
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))
`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.