Python Forum
except condition in not executing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
except condition in not executing
#1
Dear All,

The program is supposed to calculate the hour rate paid if there is an overtime and continue normally if not.
but the added while loop and try except in case there is a wrong input from the user. the user can try two times before the program closes.
The program runs except for the else part in the except . It suppose to print (close program) and quit once the user tried twice to add the input . but instead it shows the following error.

z=0
while (z<2):
    x=input("Work Hours: ")
    y= input("Hour Rate :")
    try:
        xx=float(x)
        yy=float(y)
        break
    except:
        if z < 2:
            print("Try again, Please Write a numeric input")
            z+=1
        else:
            print("Close program")
            quit()
if xx > 40 :
    Pay=xx*yy
    Ovt=(xx-40.0)*(yy*0.5)
else:
    Pay=xx*yy
print("Pay:", round(Pay)
Error:
Work Hours: dsds6 Hour Rate :dscsd Try again, Please Write a numeric input Work Hours: dss Hour Rate :sd Try again, Please Write a numeric input Traceback (most recent call last): File "C:\Users\maha\Desktop\Full Stack Web developement Courses\University of MICHIGAN\2-Python for Everybody Specialization\1-Programming for Everybody (Getting Started with Python)\Practice Folder\Pay_Rate.py", line 19, in <module> if xx > 40 : NameError: name 'xx' is not defined
Reply
#2
What happens if x is not a number, is xx set to a value? If xx is not set to a value, is the variable xx defined? If xx is not a defined as a variable what happens when you try to compare xx > 40?
Reply
#3
the specific exception would be ValueError
use sys.exit to quit

import sys


z=0
while (z<2):
    x=input("Work Hours: ")
    y= input("Hour Rate :")
    try:
        xx=float(x)
        yy=float(y)
        break
    except ValueError:
        if z < 2:
            print("Try again, Please Write a numeric input")
            z+=1
        else:
            print("Close program")
            sys.exit(-1)
if xx > 40 :
    Pay=xx*yy
    Ovt=(xx-40.0)*(yy*0.5)
else:
    Pay=xx*yy
print("Pay:", round(Pay)
Reply
#4
(Oct-26-2020, 02:52 AM)deanhystad Wrote: What happens if x is not a number, is xx set to a value? If xx is not set to a value, is the variable xx defined? If xx is not a defined as a variable what happens when you try to compare xx > 40?

Well, when the user inserts a string value and assign it to x then go to xx =float(x) it returns an error because the x value can not be changed from String to float value so there is no value assigned to xx. Then when trying to compare xx>40 it can not see the variable but this error is caught by the exception which returns "Try again, Please write a numeric value".

the issue here is that the exception can not see the else conditions if the user exceeded the allowed numbers of Attempts which is 2.

I don't want is to print "Try again, Please write a numeric value" instead I need it to print "Close Program" then quit the program.
Reply
#5
(Oct-26-2020, 10:14 AM)Larz60+ Wrote: the specific exception would be ValueError
use sys.exit to quit

import sys


z=0
while (z<2):
    x=input("Work Hours: ")
    y= input("Hour Rate :")
    try:
        xx=float(x)
        yy=float(y)
        break
    except ValueError:
        if z < 2:
            print("Try again, Please Write a numeric input")
            z+=1
        else:
            print("Close program")
            sys.exit(-1)
if xx > 40 :
    Pay=xx*yy
    Ovt=(xx-40.0)*(yy*0.5)
else:
    Pay=xx*yy
print("Pay:", round(Pay)

Does it work with you? I tried your approach and I still have the same error.
as if it keeps running the rest of the code ignoring the else statement .
Reply
#6
(Oct-26-2020, 12:43 PM)MahaEbrahim Wrote: the issue here is that the exception can not see the else conditions if the user exceeded the allowed numbers of Attempts which is 2.

Think about what happens after the user gets their second ValueError. You increment the variable z so that it is now equal to 2, then your while loop on line 5 starts over. Since the while condition is no longer True (z is no longer less than 2), the while loop is skipped and execution resumes at line 19.
Larz60+ likes this post
Reply
#7
I believe I can not simply add an if..else statement to the except part of the exception handling.
There is another way to do what I want the code to do. What do you think?
Reply
#8
(Oct-26-2020, 12:51 PM)GOTO10 Wrote:
(Oct-26-2020, 12:43 PM)MahaEbrahim Wrote: the issue here is that the exception can not see the else conditions if the user exceeded the allowed numbers of Attempts which is 2.

Think about what happens after the user gets their second ValueError. You increment the variable z so that it is now equal to 2, then your while loop on line 5 starts over. Since the while condition is no longer True (z is no longer less than 2), the while loop is skipped and execution resumes at line 19.

I can see it now. I changed the else to if . and it worked.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020