0

Eorror Here I am trying to get this code to run and accept decimals in the input/avoids the EOFError as the program calls an syntax if I remove the except code which I do not need. Is there another way I should be writing the loop?

def getWage(self):
    while True:
        try:
            self.wages = float(input("Enter your hourly wage: "))
            if self.wages <= 0:
                print("Value must be a number greater than 0!")
                continue
        except EOFError:
            print("Value must be a number greater than 0!")
            continue
        else:
            return self.wages

Whole code currently:

def getWage(self):
    while True:
        try:
            self.wages = float(input("Enter your hourly wage: "))
            if self.wages <= 0:
                print("Value must be a number greater than 0!")
                continue
        except EOFError:
            print("Value must be a number greater than 0!")
            continue
        else:
            return self.wages
Avi
  • 21
  • 5
  • 1
    post your stacktrace – drum Dec 20 '20 at 02:27
  • Please post a [mre] and please post the error. And how do you call the function? – ppwater Dec 20 '20 at 02:32
  • Fixed and added my whole code. – Avi Dec 20 '20 at 02:35
  • 2
    `EOFError` is not the exception you get if they don't type a float. You get `ValueError`. – Barmar Dec 20 '20 at 02:44
  • The code works either way. I am asking if there is a way I can write this code without needing the "except" line in the loop – Avi Dec 20 '20 at 02:45
  • If I the remove the except line it will not run. – Avi Dec 20 '20 at 02:46
  • 1
    You can't have a `try` without an `except`. It's a syntax error, because it would make no sense. (Unless you're using `finally`, but I doubt you need that here.) If you don't want an `except`, you'll have to alter your code to not use a `try` either. – CrazyChucky Dec 20 '20 at 02:57
  • And just to reiterate what Barmar noted, you'll never get an EOFError ("end of file") from the above code, so your `except` will never be executed. You definitely want `ValueError`. Your code does not work right now; if you supply invalid input, the program crashes. – CrazyChucky Dec 20 '20 at 03:00
  • You should be catching **both** `EOFError` and `ValueError` in your code. – costaparas Dec 20 '20 at 03:03
  • Oh, that's right, this could throw `EOFError` if you're on Python 2. But if you're on Python 2, the better solution would probably be to use [raw_input](https://stackoverflow.com/a/15129556/12975140) instead. If you're in Python 3, there's no way to get `EOFError` here. – CrazyChucky Dec 20 '20 at 03:09
  • I tired it with just a regular While loop but it stops working if I enter a letter instead of a number – Avi Dec 20 '20 at 03:13
  • Right... That's the purpose of your `try`/`except` clause. Is there a reason you need to avoid it? (Also if you're using Python 2, as it seems, you really should try and see if you have Python 3, or can install it. Python 2 is no longer officially supported, and 3 has cool things that will make your life easier.) – CrazyChucky Dec 20 '20 at 03:14
  • Because its not necessary and just functions as extra/useless code. My code works I so if I have to keep it then I will but I'd like to trim it if I can. – Avi Dec 20 '20 at 03:16

1 Answers1

2

The exception you catch should be ValueError when the input value is not a float as expected, not an EOFError. But to answer your question, yes, you need to check for ValueError because of your explicit conversion of the input value to float in your loop. If you try to pass a non-numeric value such as a string you will get ValueError: could not convert string to float: 'one hundred dollars'.

You could rewrite your loop so you don't explicitly convert the input value to a float(). For example, you could check if self.wages.isnumeric() in an if statement instead of using an except in a try block.

def getWage(self):
    while True:
        self.wages = input("Enter your hourly wage: ")
        if not self.wages.replace(".", "", 1).isdigit() or float(self.wages) <= 0:
            print("Value must be a numeric value greater than 0!")
            continue
        else:
            return self.wages

EDIT: For the purposes of the above program, this doesn't work. self.wages.isnumeric() or self.wages.isdigit() will report a false negative because they will read the float as a string. The modified version above can produce a hacky solution where self.wages.replace(".", "", 1).isdigit() replaces the dot in the input with whitespace and then reads it as an int to validate it's a number. However, the best practice would just be to catch the ValueError in a try block when coverting the input to a float().

def getWage(self):
while True:
    try:
        self.wages = float(input("Enter your hourly wage: "))
        if self.wages <= 0:
            print("Value must be a number greater than 0!")
            continue
    except ValueError:
        print("Value must be an integer or a float!")
        continue
    else:
        return self.wages
bpiekars
  • 53
  • 5