+ 1

How to rewrite this code without a while True loop?What should be instead of True?

The loop should be a while loop and i dont think for loops would work here but the question is what should i write instead of True? https://code.sololearn.com/cvVAYPF9TsE0/?ref=app The code itself is about an american who visits a european sauna and has to enter the temperature in farenheit, the conditions and the functions works fine and its just that im not allowed to use an infinite loop.

15th Feb 2022, 9:41 PM
Lenoname
9 Respuestas
+ 1
def fahr_to_cel(fah): y = (fah - 32) * 5 / 9 return y def get_fahr(): degrees = None try: degrees = int(input('Choose a temperature between 82-87 degrees Celsius: ')) except: print("\nInput error") return degrees f = get_fahr() c = None if f is None else fahr_to_cel(f) while f is None or (not (82 <= c <= 87)): print("Wrong input: {0}F => {1}C".format(f,c)) f = get_fahr() c = None if f is None else fahr_to_cel(f) print("\nYou can now enter. The temperature will be adjusted shortly.")
16th Feb 2022, 7:56 AM
Brian
Brian - avatar
+ 1
Brian Works!!Just one thing when i type in letters instead of numbers, i get two things printed out: input error and wrong input, only one of those should be printed out,any suggestions?
16th Feb 2022, 12:56 PM
Lenoname
+ 1
Brian nevermind i replaced input error with ”Numbers only!”
16th Feb 2022, 1:59 PM
Lenoname
+ 1
Lenoname my revision initially gets the input and calculates. Then the loop condition is a validity check. It only enters the loop if it needs to prevent moving forward until the input is valid. Both f and c mean the same as your original code, although they may also take the additional value of None to indicate invalid input.
16th Feb 2022, 7:07 PM
Brian
Brian - avatar
0
Would this be accepted? valid = False while not valid: try: f = int(input('Choose a temperature between 82-87 degrees Celsius: ')) c = fahr_to_cel(f) valid = 82 <= c <= 87 if not valid: print("Wrong input: {0}F => {1}C".format(f,c)) except: print("\nInput error") print("\nYou can now enter. The temperature will be adjusted shortly.")
16th Feb 2022, 2:58 AM
Brian
Brian - avatar
0
Brian probably not, i was thinking maybe while not 82 <= c <= 87: would be a good start but i dont really know
16th Feb 2022, 4:10 AM
Lenoname
0
Brian https://code.sololearn.com/cAJ9o2cL1bVf/?ref=app This was my attempt but if i write letters on the first try(notice only letters not numbers) there wont be anymore tries and the programme would exit or stop.
16th Feb 2022, 4:19 AM
Lenoname
0
Brian why is c and f defined outside of the loop though?and what is exactly c?
16th Feb 2022, 6:43 PM
Lenoname