在python上运行以下命令

时间:2013-11-25 01:37:32

标签: python

尝试获取一个程序输入学生姓名和分数,测试它以确保分数为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")

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

您的datatpe不匹配

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