-5

I have this code pasted below, essentially I'm trying to make it loop If the user inputs in an invalid test score(cause lets face it you cant have a negative test score or a score over 100%). So essentially I'm trying to have the program loop if they do enter an invalid score and do so until they enter one that is within the range of 0 to a 100(inclusive).

Code:

A='A'

B="B"

C="C"

D="D"

F="F"

score=float(input("Enter a test score: "))

def determine_grade(score):
    while score >= 0 and score < 100:
        if score >=90 and score <=100:
            print(A)

        elif score >=80 and score <=89:
            print(B)

        elif score >=70 and score <=79:
            print(C)

        elif score >=60 and score <=69:
            print(D)

        elif score < 60:
            print(F)
        
        return score

    score = float(input("Invalid score range, please input it again: "))

    
determine_grade(score) 
    

so far my output looks like this:

Enter a test score: -2

Invalid score range, please input it again: -2

and then it stops there, I need it to continue to loop until I get a value that's between the 0 and 100(inclusive)

9
  • 5
    Sure it does, you return after the first iteration Commented Feb 27, 2015 at 19:45
  • @61612: Hu? I don't know how it even makes it into the loop. Commented Feb 27, 2015 at 19:47
  • 2
    As @61612 said, check the indentation level for return score. Commented Feb 27, 2015 at 19:47
  • 1
    If you're using Python 2, you should replace input with raw_input. Commented Feb 27, 2015 at 19:48
  • @BenjaminBannier the final line in the script is a call to determine_grade(score) Commented Feb 27, 2015 at 19:48

2 Answers 2

2
  1. Use raw_input() Python 2.x or input() Python 3.x to get values from user.
  2. Do exception handling for ValueError if user enters a nonnumerical value.
  3. Do Type casting from string to float.
  4. Validate user input is in between 0 and 100.
  5. If 2 and 4 are True then do not break loop, ask again to enter valid number.
  6. break statement will exit while loop i.e. code comes out of respective loop.
  7. Use if and elif loop to print class according to user input.

code:

A='A'
B="B"
C="C"
D="D"
F="F" 

def determine_grade():
    while 1:
        try:
            score = float(raw_input("Enter score: "))
            if score < 0 or score > 100:
                print "Number in between 0 and 100"
            else:
                break
        except ValueError:
            print "Wrong input. Only float numbers"

    if score >=90 and score <=100:
        print(A)
    elif score >=80 and score <=89:
        print(B)
    elif score >=70 and score <=79:
        print(C)
    elif score >=60 and score <=69:
        print(D)
    elif score < 60:
        print(F)
    return score

determine_grade() 

output:

vivek@vivek:~/Desktop/stackoverflow$ python 28.py
Enter score: we
Wrong input. Only float numbers
Enter score: -8
Number in between 0 and 100
Enter score: 101
Number in between 0 and 100
Enter score: 56
F
Sign up to request clarification or add additional context in comments.

1 Comment

@Cristian Ciupitu: Thanx. more clear. Apology to grammar and spelling mistakes. :)
2

You have your detailed tests within the while loop. Then while loop should be used to get a valid score:

score=float(input("Enter a test score: "))

def determine_grade(score):
    while score < 0 or score > 100:
        score = float(input("Invalid score range, please input it again: "))

    if score >=90 and score <=100:
        print('A')

    elif score >=80 and score <=89:
        print('B')

    elif score >=70 and score <=79:
        print('C')

    elif score >=60 and score <=69:
        print('D')

    elif score < 60:
        print('F')

    return score

determine_grade(score) 

Edit:

Lets move stuff around (aka refactoring)

Lets move the main program statements together:

def enter_and_validate_test_score():
    score = float(input("Enter a test score: "))
    determine_grade(score)

enter_and_validate_test_score()

But the determine_grade function is the wrong name for what it does.

The procedure should be to get a valid score first, then print the grade based on that score:

def enter_and_validate_test_score():
    score = float(input("Enter a test score: "))
    score = validate_score(score)
    print_grade(score)

This leaves us with validate_score and print_grade

def validate_score(score):
    while score < 0 or score > 100:
        score = float(input("Invalid score range, please input it again: "))
    return score    

def determine_grade(score):
    if score >= 90 and score <= 100:
        print('A')

    elif score >= 80 and score <= 89:
        print('B')

    elif score >= 70 and score <= 79:
        print('C')

    elif score >= 60 and score <= 69:
        print('D')

    else:
        print('F')

Also note the final else:

So finally the whole program is:

def validate_score(score):
    while score < 0 or score > 100:
        score = float(input("Invalid score range, please input it again: "))
    return score    

def determine_grade(score):
    if score >= 90 and score <= 100:
        print('A')

    elif score >= 80 and score <= 89:
        print('B')

    elif score >= 70 and score <= 79:
        print('C')

    elif score >= 60 and score <= 69:
        print('D')

    else:
        print('F')

def enter_and_validate_test_score():
    score = float(input("Enter a test score: "))
    score = validate_score(score)
    print_grade(score)

enter_and_validate_test_score()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.