虽然在Python中初始化变量并不是必要的,但我的教授还是让我们练习这个。我写了我的程序,它也可以正常工作,但当我尝试初始化一些变量时,在运行它时出现了错误消息。以下是我程序的第一部分:
def main():
grade_1, grade_2, grade_3, average = 0.0
year = 0
fName, lName, ID, converted_ID = ""
infile = open("studentinfo.txt", "r")
data = infile.read()
fName, lName, ID, year = data.split(",")
year = int(year)
# Prompt the user for three test scores
grades = eval(input("Enter the three test scores separated by a comma: "))
# Create a username
uName = (lName[:4] + fName[:2] + str(year)).lower()
converted_id = ID[:3] + "-" + ID[3:5] + "-" + ID[5:]
grade_1, grade_2, grade_3 = grades
错误信息:
grade_1, grade_2, grade_3, average = 0.0
TypeError: 'float' object is not iterable
grade_1, grade_2, grade_3, average = 0.0, 0.0, 0.0, 0.0
- sobolevngrade_1 = grade_2 = grade_3 = average = 0.0
- Kenji Noguchi