gpt4 book ai didi

python try and except 函数内部的值错误

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

nameList = []
scoreList = []
gradeList = []
run = 1
loop = 1

定义输入函数():

className = input("\nWhat is the class name: ")
topic = input("What is the topic: ")
while run == 1:

studentName = input("What is the students name? Enter 'done' to exit: ")

if studentName in ("done", "Done"):
break

try:
studentScore = int(input("What is the student's score? "))
except ValueError:
print("nonono")

if studentScore <50:
grade = "NA"
if studentScore >49 and studentScore <70:
grade = "A"
if studentScore > 69 and studentScore < 90:
grade = "M"
if studentScore > 89:
grade = "E"
nameList.append(studentName)
scoreList.append(studentScore)
gradeList.append(grade)
print("\nClass Name:",className)
print("Class Topic:",topic)

我正在尝试使用 try and except 变量“studentScore”,但是它给了我错误“名称'studentScore'未定义”感谢任何帮助

最佳答案

您可以使用 while 循环不断询问用户输入有效整数,直到用户输入一个:

while True:
try:
studentScore = int(input("What is the student's score? "))
break
except ValueError:
print("Please enter a valid integer as the student's score.")

关于python try and except 函数内部的值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52416624/

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