0

I am trying to break a while loop with user input "exit" in a try-except block where the input is converted to float.

My code currently looks like this:

def checkGrade():
while True:
    try:
        floatGrade = float(input("Enter student grade: "))
        if floatGrade == 100:
            return("A+")
        # I go down the list of grades to F after this if.
    except:
        if floatGrade == "exit": 
    # This is where I want the code to break, but it gives me an error when I print the function def checkGrade()
            break
        else:
            continue

I get an error

could not convert string to float: "exit"

Any thoughts? Thanks in advance for your help!

keikai
  • 14,085
  • 9
  • 49
  • 68

2 Answers2

1

I would restructure the code to collect the input first and try to convert it to a float in the try/except, so if it fails the original input value is kept.

def checkGrade():
    while True:
        floatGrade = input("Enter student grade: ")
        try:
            floatGrade = float(floatGrade)
        except ValueError:
            if floatGrade == "exit":
                break
        else:
            if floatGrade == 100:
                return("A+")
alec
  • 5,799
  • 1
  • 7
  • 20
  • Thanks! I asked the guy above the same question, but when I run the program and print now, it returns None before exiting the program. Is there anyway to suppress this output? I'm trying to get this to loop until the user inputs "exit" - after which the program quits. – Chris Macras Mar 18 '20 at 03:30
  • All functions return `None` by default. You can choose to only print if it contains a value. For example, `grade = checkGrade()` `if grade is not None: print(grade)` – alec Mar 18 '20 at 03:37
  • That's really good to know. Working great now. Thanks for all of your help! – Chris Macras Mar 18 '20 at 03:55
0

There is no point of putting continue in except block as the loop will automatically continue.

def checkGrade():
       while True:
           try:
                floatGrade = input("Enter student grade: ")
                if float(floatGrade) == 100.0:
                     return("A+")
                # I go down the list of grades to F after this if.

           except:
                if floatGrade == "exit": 
                     print('breaking the loop with code exit')
                     break
                else:
                     print('please enter only float values...')


 res=checkGrade()


 #added here
 if res:
    print(res)
 else:
    print('You chose the exit command !!')

Suggestion :

for checking the value between 2 value, you may use

if number >= 10000 and number <= 30000:

But we can optimize it further with

if 10000 <= number <= 30000

for more

halfer
  • 19,824
  • 17
  • 99
  • 186
teddcp
  • 1,514
  • 2
  • 11
  • 25
  • Awesome! Thanks for the response. One follow-up, when I print the function, it returns None before ending the program. Is there any way to suppress this side effect? I'm trying to get this program to loop until the user inputs "exit" if that helps! – Chris Macras Mar 18 '20 at 03:28
  • @ChrisMacras,I have updated the code,please refer now. Hope it helps :) – teddcp Mar 18 '20 at 04:05
  • @ChrisMacras, sorry for the late response – teddcp Mar 18 '20 at 04:08