Since I used isdigit() method to verify the input, any floating number input won’t be processed because of the “.” in the number. I suppose to use type conversion with “try, except” method instead.
# calculate pay(hours worked and hourly rate ) by declaring function
def CalPay(hrs, rate):
if hrs <= 40:
pay = hrs * rate
elif hrs <= 60:
pay = 40 * rate + (hrs – 40) * rate * 1.5
else:
pay = 40 * rate + 20 * rate * 1.5 + (hrs – 60) * rate * 2
return pay
while True:
# Prompt user for number of hours worked and validate input
hrs_input = input(“Please enter number of hours worked for this week: “)
if not hrs_input.isdigit(): # check if input is non-negative integer
#str.isdigit() means
#a function that verifies that a string consists of only ‘numeric’ characters.
#if there is even one letter, return False.
#it returns True if all characters consist of only ‘numbers’
print(“You entered wrong information for number of hours.”)
continue # Message the user again if input is incorrect
hrs = float(hrs_input)
while True:
# let users to enter for hourly rate and then validate input
rate_input = input(“What is hourly rate? “)
if not rate_input.isdigit(): # check if input is a positive integer
print(“You entered improper information for the rate.”)
continue # Message the user again if input is incorrect
rate = float(rate_input)
break
# Calculate – Display pay
pay = CalPay(hrs, rate)
print(‘Your pay for this week is:
# ask users if they want to repeat the calculation
choice = input(‘Do you want another pay calculation? (y or n) ‘)
if choice == ‘n’: # if user does not want to repeat, break the loop
print(“Good bye!”)
break
this is my code and plz edit it to run code well. just edit is.digit() -> try except