-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)

Community
  • 1
  • 1
YoungSon
  • 7
  • 3
  • 5
    Sure it does, you return after the first iteration – vaultah Feb 27 '15 at 19:45
  • @61612: Hu? I don't know how it even makes it into the loop. – Benjamin Bannier Feb 27 '15 at 19:47
  • 2
    As @61612 said, check the indentation level for `return score`. – Cristian Ciupitu Feb 27 '15 at 19:47
  • 1
    If you're using Python 2, you should replace `input` with `raw_input`. – Cristian Ciupitu Feb 27 '15 at 19:48
  • @BenjaminBannier the final line in the script is a call to `determine_grade(score) ` – aruisdante Feb 27 '15 at 19:48
  • @aruisdante: This doesn't look like `do .. while`. – Benjamin Bannier Feb 27 '15 at 19:49
  • So your saying put return score after my score=input statement? – YoungSon Feb 27 '15 at 19:50
  • @BenjaminBannier It's not, they assign to `score` based on user input before the function definition. However in our hast to answer their immediate question (which was caused by a typo), we missed the larger problem of what they were trying to actually *do* with this function: prompt for user input until some correct input is provided. This question is actually a duplicate of http://stackoverflow.com/questions/8114355/loop-until-a-specific-user-input and many other similar. The answers that were provided before the lock caught this. – aruisdante Feb 27 '15 at 21:08
  • Voted to reopen. The programming problem the OP has is one that many rookies have which is to determine how control statements affect program flow. This problem might have been averted by `programming by intention` and can be illustrated when I edit my answer to refactor the one function into several explaining functions. – quamrana Feb 28 '15 at 18:39

2 Answers2

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
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
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()
quamrana
  • 37,849
  • 12
  • 53
  • 71