I have a program the functions correctly but I have to add error checking and now I am running into an issue. The problem is that if you type in a number for the worker_name then the following time you type in a name, once you go through all of the input fields it prints out the "Production worker information" twice. How do I fix this?
Entire program:
class Employee(object):
def __init__(self, name, id_number):
self.id_number = id_number
self.name = name
class Worker(Employee):
def __init__(self, name, id_number, shift_number, pay_rate):
#call superclass __init__ method
Employee.__init__(self, name, id_number)
#initialize shift_number and pay_rate attributes
self.shift_number = shift_number
self.pay_rate = pay_rate
def main():
#variables
worker_name= " "
worker_id = " "
worker_shift = 0
worker_pay = 0.00
#get data attributes
while 1:
try:
worker_name = input("Enter the worker name: ")
print()
if not worker_name.isalpha():
print("Only letters are allowed!")
print()
main()
break
worker_id = int(input("Enter the ID number: "))
print()
worker_shift = int(input("Enter the shift number: "))
print()
worker_pay = float(input("Enter the hourly pay rate: "))
print()
break
except Exception as e:
print("Invalid choice! try again! " + str(e))
print()
#create an instance of Worker
Employee.worker = worker_name, worker_id, worker_shift, worker_pay
if not worker_name.isalpha():
pass
#display information
print ("Production worker information ")
print("---------------------------------")
print()
print ("Name: ", worker_name)
print()
print ("ID number: ", worker_id)
print()
print ("Shift: ", worker_shift)
print()
print ("Hourly Pay Rate: $ " + format(worker_pay, ",.2f"))
main()
You are recursively calling main()
instead of allowing the while 1:
loop to work.
I think you meant:
if not worker_name.isalpha():
print("Only letters are allowed!")
print()
continue
The continue
will jump back to the while 1:
You shouldn't call again main()
inside itself, use you try/except
system
def main():
#variables
worker_name, worker_id, worker_shift,worker_pay = "", "", 0, 0
while True:
try:
worker_name = input("Enter the worker name: ")
if not worker_name.isalpha():
print("\nOnly letters are allowed!")
continue
worker_id = int(input("\nEnter the ID number: "))
worker_shift = int(input("\nEnter the shift number: "))
worker_pay = float(input("\nEnter the hourly pay rate: "))
break
except Exception as e:
print("Invalid choice! try again! " + str(e), "\n")
Use the loop to retry the input if it fails (rather than calling recursively). You can also take advantage of try/catch
to make the code quite a bit shorter -- just try
the whole thing you want to do in order to create the worker
and then you don't need to separately declare and check each variable one at a time.
class Employee:
def __init__(self, name: str, id_number: int):
assert name.isalpha(), "Only letters are allowed in employee names!"
self.id_number = id_number
self.name = name
class Worker(Employee):
def __init__(
self,
name: str,
id_number: int,
shift_number: int,
pay_rate: float
):
# call superclass __init__ method
Employee.__init__(self, name, id_number)
# initialize shift_number and pay_rate attributes
self.shift_number = shift_number
self.pay_rate = pay_rate
def main():
# create an instance of Worker
while True:
try:
name = input("\nEnter the worker name: ")
assert name.isalpha(), "Only letters are allowed!" # fail fast!
worker = Worker(
name,
int(input("\nEnter the ID number: ")),
int(input("\nEnter the shift number: ")),
float(input("\nEnter the hourly pay rate: "))
)
break
except Exception as e:
print(f"Invalid choice! try again! {e}\n")
# display information
print(f"""
Production worker information
---------------------------------
Name: {worker.name}
ID number: {worker.id_number}
Shift: {worker.shift_number}
"Hourly Pay Rate: ${worker.pay_rate:.2f}
""")
main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.