0

trying to get a program to enter a students name and score, test it to make sure score is a vaule >=0 and <=100 and save results to a file and loop back

 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
  • gradeFile.write(Score+"\n") gives you error TypeError: unsupported operand type(s) for +: 'float' and 'str', you can change it to gradeFile.write(str(Score)+"\n") Commented Nov 25, 2013 at 1:42
  • You didn't ask a question. What question about your task do you need answered? Commented Nov 25, 2013 at 2:00

2 Answers 2

1

You need to have a way to exit the loop. For your outer loop, you automatically go in. Then you loop again until you get a valid score, via your inner loop, and you repeat. In your current configuration, there's no way to exit the loop.

Additionally, score should be a number, but you enter it as a string in Score = "0". When outputting, you're going to want to write str(Score) so that you can concatenate it with "\n".

I suggest that your outer loop have something like while Score >= 0 and userWantsToContinue. You can handle userWantsToContinue in whatever way you see fit.

Sign up to request clarification or add additional context in comments.

Comments

1

Your datatpe doesn't match

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.