gpt4 book ai didi

python - 尝试创建一个简单的成绩册程序

转载 作者:行者123 更新时间:2023-12-01 06:42:57 25 4
gpt4 key购买 nike

这是我到目前为止的代码。当我输入 5 个数字后,它一直循环到开头。

创建成绩簿程序,允许用户指定可能的最高成绩,然后输入个人作业成绩。输入的成绩不能超过指定的最高值,也不能为负成绩。最后也想平均成绩

def gradebook():
while True:
print("what is the highest grade possible")
inp = int(input())
if inp == "":
cap = inp
gradebook = []
for x in range(5):
l = int(input("enter grade"))
if l > cap:
print("you cannot have a grade higher than " + str(cap))
gradebook.append(l)
print(gradebook)

最佳答案

while 循环对于这种情况是正确的,在本例中是嵌套的 while 循环。

代码

def gradebook(number_of_assignments):
# init
assignment_grade_list = []
assignment_count = 1

while True:
try:
# highest grade
highest_grade = int(input("What is the highest grade possible?:"))
except ValueError:
print("Invalid input. The highest grade must be numeric.")
continue
else:
while assignment_count in range(1,number_of_assignments+1):
try:
# assignment grade
assignment_grade = int(input("Enter assignment #%s grade:" % str(assignment_count)))
except ValueError:
print("Invalid input. The assignment grade must be numeric.")
continue
if assignment_grade > highest_grade:
print("Invalid input. You cannot have a grade higher than %s." % str(highest_grade))
continue
if assignment_grade < 0:
print("Invalid input. You cannot have a grade lower than zero.")
continue
else:
assignment_grade_list.append(assignment_grade)
assignment_count+=1
else:
# average assignments
return round(sum(assignment_grade_list)/len(assignment_grade_list),2)
break

# gradebook
a = gradebook(5)
print('Average assignment score: %s' % a)

关于python - 尝试创建一个简单的成绩册程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59365282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com