Dec-04-2020, 04:39 PM
Hi guys, I just got the clothing recommendation app done. On the part where it says how much rain is outside today and how much is present today. The code is not working correctly since I used try and except statements and I want it to keep on asking until a valid input is typed in. I would need some help on those last two portions of the code and it could be useful for me to fix my other python assignments.
# This asks what's the current temperature outside and when the user types something # other than the numbers of 0-120 it will keep prompting the user until a valid input # is entered. while True: try: temp = int(input("Enter the temperature outside: ")) break except: print("Invalid input, please try again:") # This part checks the temperature that the user inputs to see what's considered cold, normal or hot. It also gives out the clothing that the user should wear. if temp > 0 and temp < 40: print("It is cold outside") print("You should wear a sweater, jeans, and a scarf") elif temp > 50 and temp < 60: print("It is normal outside") print("You should wear a t-shirt and jeans") elif temp > 60 and temp < 90: print("It is a nice day outside") print("You should wear a t-shirt and jeans") elif temp > 90 and temp < 120: print("It is hot outside") print("A tank top and shorts is recommended for this type of temperature") # This asks if it's raining outside while True: try: rain = input("Is it raining outside? ") # Checks to see if the user inputed yes or no, it should give the recommended clothing to wear. When the user types no, it should give that nothing else is required to wear if rain == "yes": print("You should wear a rain jacket and rain proof shoes.") break else: print("Nothing else needed to be worn today") break except: print("Invalid answer, please try again:") # Checks to see how much sun is present and the user is supposed to type not much or not little to get the code to stop, if they put a lot as their anwser it should recommend the user to wear sunscreen while True: try: sun = input("How much sun is present today? ") if sun == "not much" or sun == "a little": print("No sunscreen required") break elif sun == "a lot": print("You should put sunscreen today") break except: print("Invalid input, please try again:")