2013-11-25 95 views
0

试图让一个程序来输入学生姓名和分数,对其进行测试,以确保评分以下是vaule> = 0和< = 100,将结果保存到一个文件和环回运行在蟒蛇

gradeFile = open("grade.dat","a") 
    Score = "0" 
    while Score>=0: 
     Name = raw_input("What is the students's name?: ") 
     Score = float(raw_input("What is the students's score?: ")) 
     while Score <0 or Score >100 : 
      print("ERROR: the grade cannot be less than 0 or more than 100") 
      Score = float(raw_input("What is the students's score?: ")) 
     gradeFile.write(Name+"\n") 
     gradeFile.write(Score+"\n") 
    gradeFile.close() 
    print("Data saved to grade.dat") 
+0

gradeFile.write(得分+ “\ n”)为您提供了错误'类型错误:对于不支持+操作数类型(S): '浮动' 和“str'',您可以将其更改为gradeFile.write(str(Score)+“\ n”) – ton1c

+0

您没有提出任何问题。你需要回答什么问题? – user2357112

回答

1

你需要有一种方法来退出循环。对于你的外部循环,你会自动进入。然后你再次循环,直到你得到一个有效的分数,通过你的内部循环,然后重复。在您当前的配置中,无法退出循环。

此外,分数应该是一个数字,但是您在Score = "0"中将其作为字符串输入。输出时,您要编写str(Score),以便您可以将它与"\n"连接起来。

我建议你的外环有类似while Score >= 0 and userWantsToContinue的东西。您可以以任何您认为合适的方式处理userWantsToContinue

1

您datatpe不匹配

Score = "0" # So, score is a string 
while Score >= 0: # Oh, thenm it's a integer?