+ 1
How to return code to top line if user makes input less than 40. I tried like this but it doesn't work. Thank you all in advance
hour=float(input("how many hours:")) rate=float(input("rate per hour")) def work(hour,rate): if hour >= 40: print("overtime") nor = hour*rate pvm = (hour - 40.0) * (rate * 0.5) o = nor + pvm print(f'Normal shift:{nor:"^9},Overtime:{pvm:*<9},Alltogether:{o}') if rate<40: print("input more than 40") work(hour,rate) else: exit() x=work(hour,rate) print(x)
4 ответов
+ 2
hour = 0
while hour < 40:
hour=input("enter hours (>=40)")
# from here you have a valid value for hour
...
+ 7
Srki ,
from my point of view it doesn't make too much sense to get the input for `hour` in the main program, and then check it for correctness in the function.
better to try this:
to make sure that the input value for `hour` gets a correct value, you can run the input procedure in a loop. if input value is ok, exit the loop, otherwise ask again for input.
doing so, we can omit: if rate<40:, also the related else branch.
since the ouput of the calculation is done inside the function and nothing is returned to the caller, work(hour,rate) will be the last line that will execute.
+ 5
Srki ,
can you provide a task description ?
+ 2
Thank you all for your help 🙌🏻.