I'm writing a pay calculator program using a series of nested loops. Everything works except I have to make sure that the input for number of hours worked on the specified day is between 0 and 24 inclusive. This is the code I have for that section, I've tried multiple different options but they all either crash the program or aren't recognised at all. Any help would be appreciated!
This is the related code:
for x in range(0, weeks):
for y in days:
while True:
print ("Enter the number of hours for Week", x+1, y, ":")
try:
hours = int(input())
except ValueError:
print ("Invalid: Enter a positive integer")
continue
else:
break;
if y == 'Saturday':
newRate = satRate
elif y == 'Sunday':
newRate = sunRate
else:
newRate = baseRate
rate += (hours * newRate)
This is the whole code if you need a more wide look:
baseRate = -1
while baseRate < 1:
baseRate = float(input("Enter the base pay rate: "))
if baseRate < 1:
print("Invalid: Enter a non-negative amount")
satRate = baseRate * 1.5
sunRate = baseRate * 2
pay = 0
rate = 0
hours = -2
weeks = -1
while weeks < 1:
while True:
try:
weeks = int(input("Enter the number of weeks: "))
except ValueError:
print("Invalid: Enter a positive integer")
continue
else:
break
if weeks < 1:
print("Invalid: Enter a positive integer")
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for x in range(0, weeks):
for y in days:
while True:
print ("Enter the number of hours for Week", x+1, y, ":")
try:
hours = int(input())
except ValueError:
print ("Invalid: Enter a positive integer")
continue
else:
break;
if y == 'Saturday':
newRate = satRate
elif y == 'Sunday':
newRate = sunRate
else:
newRate = baseRate
rate += (hours * newRate)
pay = (round(rate, 2))
av = pay/weeks
average = round(av,2)
print("Total pay is: ", pay)
print("Average pay per week is: ", average)