[英]Sum of the product of a number and user inputs
How do I make this program work? 如何使该程序正常工作?
Problem 问题
I need to set how many number of float inputs a user can enter. 我需要设置用户可以输入多少个浮点输入。 And then multiply each input by a number and sum each product. 然后,将每个输入乘以一个数字,并对每个乘积求和。
Code 码
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)
Output should look like: 输出应如下所示:
Enter how many numbers you would like to input? 输入您想输入多少个数字? 3 3
What is the value of number 1? 数字1的值是多少? 2 2
What is the value of number 2? 数字2的值是多少? 3 3
What is the value of number 3? 3的值是多少? 1 1个
Enter the multiplicand: 5 输入被乘数:5
The total value is: 30 总价值是:30
You can do it this way: 您可以这样操作:
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)
This should solve ur prob : ` 这应该解决您的问题:
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.