【问题标题】:How do I make a program ask the user for the number of inputs, and add the result of their inputs togther?如何让程序询问用户输入的数量,并将他们输入的结果加在一起?
【发布时间】:2021-05-09 15:31:03
【问题描述】:

我目前正在尝试制作一个程序,该程序可以根据您之前获得的成绩以及它们对您的学期成绩的影响程度来计算您的期末学期成绩。问题是我正在努力弄清楚如何让程序询问用户他们获得的成绩数量,然后询问相同数量的成绩。

简而言之,该程序应该执行以下操作:

  1. 询问获得的成绩数量

  2. 询问用户获得的分数以及该分数的百分比 x 次,其中 x 是用户在第一个问题上给出的数字。

  3. 根据输入计算最终学期成绩。

我希望有人能提供帮助,到目前为止,我已经编写了这段代码,以防它帮助你理解我想要做什么。

grade1 = int(input("What was the first grade you received? " ))
percentage1 = float(input("What's the percentage of the first grade? "))
grade_value1 = grade1 * percentage1

grade2 = int(input("\nWhat was the second grade you received? "))
percentage2 = float(input("What's the percentage of the second grade? "))
grade_value2 = grade2 * percentage2

grade3 = int(input("\nWhat was the third grade you received? " ))
percentage3 = float(input("What's the percentage of the third grade? "))
grade_value3 = grade3 * percentage3

finale_grade = grade_value1 + grade_value2 + grade_value3

if finale_grade < 2.9:
    print("\nYour finale grade is 2")
elif finale_grade < 5.4:
    print("\nYour finale grade is 4")
elif finale_grade < 8.4:
    print("\nYour finale grade is 7")
elif finale_grade < 10.9:
    print("\nYour finale grade is 10")
else:
    print("\nYour finale grade is 12")

【问题讨论】:

  • 为什么要让用户计算成绩?让他们尽可能多地输入,并在他们输入空行或退出命令时计算它们。您甚至可以在每次输入后更新平均值。

标签: python


【解决方案1】:
#I hope this works , I'm a beginner tho 

num_of_recieved_grades = int(input("number of recieved 
grades ?"))
final_grade = 0

# for loop in range of the number entered

for x in range (num_of_recieved_grades ):
    grade = int(input("what is the grade"))
    percentage = float(input("what is the percentage"))
    final_grade += grade * percentage
  

【讨论】:

  • 成功了,非常感谢。我之前尝试过使用 for 循环,但它不起作用,所以我认为这是错误的方法。
  • 所有最好的兄弟
猜你喜欢
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多