0

Here is the assignment prompt:

Write a pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Use try and except so that your program handles non-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program:

Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input

Enter Hours: forty
Error, please enter numeric input

Here's how I wrote my code:

# Prompts the user to enter the hours and rates
hrs = input('Enter hours: ')

#if hours are non-numeric, give the error message
if hrs is str:
    print('Please enter a valid number for the hours or rate!')

#otherwise, proceed with the calculation
else:
    try:
        rate = input('Enter hourly rate: ')
        fh = float(hrs)
        fr = float(rate)
        if fh > 40:
            extra = float(fh - 40)
            otpay = (40*fr)+((fr*1.5)*extra)
            print('You have overtime! Your pay is: ', otpay)
        else:
            normpay = fh*fr
            print('No overtime! Your pay is: ', normpay)
#if rate is non-numeric, give the error message
    except:
        print('Please enter a valid number for the hours or rate!')

The problem I am having is that even when hours are in non-numeric form, it doesn't display the error message and moves on to prompting the user to enter their rate. For example:

Enter hours: ten
Enter hourly rate: 

I am fairly new to python, and I am not sure what exactly I am doing wrong here

sgy0003
  • 43
  • 5
  • 1
    The value from input is a str. Try checking if can be converted to an int instead. – dfundako Mar 08 '21 at 18:45
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – G. Anderson Mar 08 '21 at 18:50
  • Also, when checking the type of a variable, use a built-in method, not `is`. See [How to determine a Python variable's type?](https://stackoverflow.com/questions/402504/how-to-determine-a-python-variables-type) – G. Anderson Mar 08 '21 at 18:51

1 Answers1

0

By adding a float before the input, the value is automatic saved as a floating point number, and not a string. By doing that, you could also remove multiply lines frome the code. Down I have made a suggestion of how the code could look like with a function set-up, and try/except commands.

    def pay_computation():
        try:
            hrs = float(input("Enter hours: "))
            rate = float(input("Enter hourly rate: "))
            if hrs > 40:
                extra = float(hrs - 40)
                otpay = (40*rate)+((rate*1.5)*extra)
                print('You have overtime! Your pay is: ', otpay)
            else:
                normpay = hrs*rate
                print('No overtime! Your pay is: ', normpay)
        except ValueError:
                print("Please enter a valid number for the hours or the rate!")
                reset = input("Do you want to try again? Pleas answer with yes or no: ")
                if reset == "yes":
                    pay_computation()
                else:
                    print("Have a good day")

    pay_computation()

You could also do it even more advanced by splitting the parts up in different functions. That will let you handle more freely, and get more accurate feedback when you input a non-numeric value. Like this:

def hours():
try:
    global hrs
    hrs = float(input("Enter hours: "))        
except ValueError:
    print("Please enter a valid number for the hours!")
    reset = input("Do you want to try again? Pleas answer with yes or no: ")
    if reset == "yes":
        hours()
    else:
        print("Have a good day")


hours()


def hourly_rate():
try:
    global rate
    rate = float(input("Enter hourly rate: "))        
except ValueError:
    print("Please enter a valid number for the hourly rate!")
    reset = input("Do you want to try again? Pleas answer with yes or no: ")
    if reset == "yes":
        hourly_rate()
    else:
        print("Have a good day")


hourly_rate()


def pay_computation():
if hrs > 40:
    extra = float(hrs - 40)
    otpay = (40*rate)+(rate*1.5*extra)
    print('You have overtime! Your pay is:', otpay)
else:
    normpay = hrs*rate
    print('No overtime! Your pay is:', normpay)


pay_computation()