和我一起,第一次在这里发帖,我有大约 6 周的 python 知识。
我们上周的任务是编写一个程序,让用户输入他们参加了多少次测验,该测验的成绩,然后打印他们的平均分和每个测验的成绩摘要。我们的教授允许我们自由地写这篇文章。我想尝试一些我们尚未涵盖的事情。一切顺利,直到本周。本周的任务是调整上周的代码。
他要求的是输入包含测验“标题”的输入。需要审查用户的输入。这是给出的例子。
我知道我可以对范围类型的东西使用递减,但我无法终生弄清楚在哪里以及如何实现它。如果我把它放在那里,我似乎会打破 while 循环。如果我进行了 3 次测验,然后输入这 3 次,它不会退出循环。
这是我的,如有凌乱请见谅。我花了一些时间在谷歌上搜索如何最好地在这里发帖所以对我放轻松 :)
name = input("Enter your first name: ")
Q = int(input("How many quizzes have you taken? "))
list1 = {}
minimum_quizzes = 2
passing_score = 70
if Q <= minimum_quizzes:
print("You need at least three quiz scores to enter.")
exit()
if Q >= minimum_quizzes:
while True:
print("Type 'exit' to exit.")
quizTitle = input("Enter your quiz title:")
if quizTitle.lower() == "exit":
break
quizGrades = input("Enter your quiz grade: \n")
if quizGrades.lower() == "exit":
break
try:
quizGrades = float(quizGrades)
quizTitle = (quizTitle)
list1.update({quizTitle :quizGrades})
except ValueError:
print("Please enter only numbers for a grade")
if len(list1) == Q:
break
if len(list1) == Q:
#list1.sort(reverse=True)
print(f"Hello {name.capitalize()}, here is your entered quiz information: ")
for k, v in list1.items():
print(f"For quiz " + str(k) + " you received " + str(v),"%")
if quizGrades >= passing_score:
print(f"Your average quiz score is {sum(list1.values()) / len(list1)}%, which is above the minimum of {passing_score}%. Congrats, you've passed!")
if quizGrades < passing_score:
print(f"Sorry, your average quiz score is {sum(list1.values()) / len(list1)}%, you need an average of {passing_score}% to pass.")
如果需要更多信息或我应该对这篇文章进行格式化编辑,请告诉我。 我感谢所有帮助,谢谢!