【问题标题】:Sum of the product of a number and user inputs数字与用户输入的乘积之和
【发布时间】:2016-02-29 05:28:32
【问题描述】:

如何让这个程序发挥作用?

问题

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

代码

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

【问题讨论】:

  • 请不要破坏您的问题。这里的问题旨在帮助未来的访客遇到同样的问题,而不仅仅是您。

标签: python python-3.x


【解决方案1】:

你可以这样做:

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

    【讨论】:

      【解决方案3】:

      这应该可以解决您的问题: `

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

      `

      【讨论】:

        猜你喜欢
        • 2014-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多