0

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?

AhmedR
  • 1
  • 3
  • you don't need to check for both `.isnumeric` and `.isdecimal` because `.isnumeric` already checks if it is a decimal and then some more: https://www.learnbyexample.org/python-string-isdecimal-method/ – Matiiss Oct 09 '21 at 20:25
  • I used just .isnumeric(), and while it did enter the loop - decimals were not accepted. – AhmedR Oct 09 '21 at 20:39
  • what you mean by decimals? `.isnumeric` checks if the string contains decimals and if it does contain only those then it returns True so it definitely did work, also for example: `if bonus_pay.isnumeric() == False` can be shortened to `if not bonus_pay.isnumeric():` – Matiiss Oct 09 '21 at 20:54
  • I entered the code you put and it still did not work. It just outputs the "Invalid Input" and then enters the while loop. I even tested removing the isnumeric() and using just isdecimal() and the same thing happened. I enter 0.10 every time but it does not accept it (but if i enter - say 4 - the code runs as accepted) – AhmedR Oct 09 '21 at 21:25
  • yes, `.isnumeric` (or `.isdecimal` or `.isdigit`) will return False if the string contains a dot, if you wan to check if the given string can be converted to float then you have to look at this: https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python – Matiiss Oct 09 '21 at 21:32
  • Thank you! This has fixed my problem! – AhmedR Oct 09 '21 at 21:42

1 Answers1

0

Your error is because you are not calling the isdecimal() method in this piece of code:

if bonus_pay.isnumeric() == False and bonus_pay.isdecimal == False:

when should it be:

if bonus_pay.isnumeric() == False and bonus_pay.isdecimal() == False: