I was recently working on some programming challenges to increase my proficiency in python until I came to a dead end with one of the challenges. The challenge is to allow the user to input: the hourly wage of a worker; the bonus pay for each item made; the number of hours worked; the number of items made. I decided to add some validation to my program but ran into an error when checking if the bonus pay was a number or decimal. My code for the program is as follows:
hourly_pay = input("Please enter the rate of pay for 1 hour: ")
if hourly_pay.isnumeric() == False:
while hourly_pay.isnumeric() == False:
print("Invalid input")
hourly_pay = input("Please enter the rate of pay for 1 hour: ")
bonus_pay = input("Please enter the bonus pay for each car made (in £'s): ")
if bonus_pay.isnumeric() == False and bonus_pay.isdecimal == False:
while bonus_pay.isnumeric() == False or bonus_pay.isdecimal() == False:
print("Invalid input")
bonus_pay = input("Please enter the bonus pay for each car made (in £'s): ")
hours_worked = input("Please enter the number of hours worked: ")
if hours_worked.isnumeric() == False or int(hours_worked) > 15:
while hours_worked.isnumeric() == False or int(hours_worked) > 15:
print("Invalid input")
hours_worked = input("Please enter the number of hours worked: ")
cars_made = input(f"Please enter the number of cars made in {hours_worked} hours: ")
if cars_made.isnumeric() == False:
while cars_made.isnumeric() == False:
print("Invalid input")
cars_made = input(f"Please enter the number of cars made in {hours_worked}: ")
total_pay = (int(hourly_pay) * int(hours_worked)) + (float(bonus_pay) * int(cars_made))
print(f"The pay owed is £{total_pay}")
The issue is with the following piece of code:
bonus_pay = input("Please enter the bonus pay for each car made (in £'s): ")
if bonus_pay.isnumeric() == False and bonus_pay.isdecimal == False:
while bonus_pay.isnumeric() == False or bonus_pay.isdecimal() == False:
print("Invalid input")
bonus_pay = input("Please enter the bonus pay for each car made (in £'s): ")
Every time I enter a letter, the if statement accepts it and does not start the loop.
I was wondering if anyone knew what I did wrong and if they could help me fix it?