1

I need to write a try/except component with Python 3 to compute the total pay by multiplying wage rate with working hours. The inputs have to be numeric, otherwise print an error message and stop.

hours = input("Enter Hours:")
try:
    hours = float(hours)
    rate = input("Enter Rate:") 
    try:
        rate = float(rate)
        if hours > 40:
            hours = hours - 40
            print("Pay:", 40*rate + hours*1.5*rate)
        else:
            print("Pay:", rate*hours)
    except:
        print("Error. Please enter numeric inputs.")
except:
     print("Error. Please enter numeric inputs.")

The codes above just keep running without prompting anything (not even the 1st line). And the below still prompts for the second input after the first fails and the whole thing should stop.

hours = input("Enter Hours:")
try:
    hours = float(hours)
except:
    print("Error. Please enter numeric inputs.")
rate = input("Enter Rate:")
try:
    rate = float(rate)
    if hours > 40:
        hours = hours - 40
        print("Pay:", 40*rate + hours*1.5*rate)
    else:
        print("Pay:", rate*hours)
except:
    print("Error. Please enter numeric inputs.")

I'm a newbie in Python, but I kinda sense there's a simple solution to this. Can somebody help me get that? Thank you very much!

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Paw in Data
  • 1,262
  • 2
  • 14
  • 32
  • 2
    Your first posted code seems to work fine for me. It definitely doesn't "keep running without prompting anything". Are you sure that it doesn't work when you run it? – Mark Snyder Jan 28 '20 at 21:46
  • If you want the program to terminate on errors, why do you even use `try/except`? Just let Python throw the error for you. Also, your second code has indentation problems. Please fix it – Tomerikoo Jan 28 '20 at 21:48
  • @MarkSnyder No, it doesn't. I tried running it in a new Jupyter Notebook, and I tried restarting ANACONDA 3 ... The chunk just keeps running without prompting anything. – Paw in Data Jan 28 '20 at 21:51
  • @Tomerikoo Unfortunately this is for a homework, and try/except is required. Also, the indentation might have messed up a little bit when I copied the codes, so I just added a screenshot. Judged by the colors, it should be fine, I suppose? – Paw in Data Jan 28 '20 at 22:01
  • No it is not OK to put sccreenshots. If you want people to help you they should be able to copy-paste your code and run it. Read about [ask] and how to provide a [mre]. [edit] your post with correct indentations. It is not very hard, copy your code and make sure it looks fine, then simply highlight it and press `ctrl+k` – Tomerikoo Jan 28 '20 at 22:08
  • And anyway, the screenshot provided is of the first code which is fine – Tomerikoo Jan 28 '20 at 22:10
  • @Tomerikoo Sorry about that. And thank you. I went to edit the post and it said you had done that already. – Paw in Data Jan 28 '20 at 22:17
  • @Tomerikoo Yes, I guess I'm allowed to use break. Thank you! – Paw in Data Jan 28 '20 at 22:18
  • Yes, but look at the way your second piece of code is formatted. I think this way is better for indentations as all the text is aligned to the left. You're welcome! – Tomerikoo Jan 28 '20 at 22:18
  • @Tomerikoo Yes, you're totally right. I won't mess up again. Thanks a lot! – Paw in Data Jan 28 '20 at 22:21

0 Answers0