Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

python - Why does this code display the error message "Error. please enter numeric input" only after entering both values of hours and rate

I want that code to diaplay this message "Error. please enter numeric input" just after entering Hours in letters....it appears after inserting both values of hours and rate....if hours are non-numeric value it proceeds to entering rate and not displaying the error message

why

inp = input('Enter Hours: ')
    inp2 = input ('Enter Rate: ')
    try:
        Hours = float (inp)
    except:
        print ('Error. please enter numeric input')
    quit()    
    try:
        Rate = float (inp2)
    except:
        print ('Error. please enter numeric input')
    quit()
    if Hours > 40 :
        Pay = (Hours * 10) + (Hours - 40) * (1.5)
        print ('Pay: ', Pay)
    elif 0 < Hours <= 40 :
            Pay = Hours * Rate
            print ('Pay= ', Pay)
question from:https://stackoverflow.com/questions/65946546/why-does-this-code-display-the-error-message-error-please-enter-numeric-input

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your use of indentation and spacing was very inconsistent. And the use of comments was not right. That probably was the issue. To learn about indentation here

The new code would be

# why (comments start with # )
inp = input('Enter Hours: ')

try:
    Hours = float(inp)
except:
    print('Error. please enter numeric input')
    quit() #  Inconsistent indenting makes unreachable (and unreadable) code.

inp2 = input('Enter Rate: ')
try:
    Rate = float(inp2)
except:
    print('Error. please enter numeric input')
    quit()

if Hours > 40 :
    Pay = (Hours * 10) + (Hours - 40) * (1.5)
    print('Pay: ', Pay)

elif 0 < Hours <= 40 :
    Pay = Hours * Rate
    print('Pay= ', Pay)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...