一个数与用户输入的乘积之和

2024-06-02 04:42:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我如何使这个程序工作?在

问题

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

编码

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的值是多少?1

输入被乘数:5

总价值是:30


Tags: the用户in程序forinput数字product
3条回答

你可以这样做:

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

`

相关问题 更多 >