Python Forum
Clothing Reccomendation app not working correctly.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Clothing Reccomendation app not working correctly.
#1
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:")    
  
Reply
#2
There is no exception thrown. You are not trying to convert input to a number, so there is no chance for an exception there. You could do this:
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
    else
      raise ValueError

  except:
      print("Invalid answer, please try again:")
Reply
#3
I tried that but this part of the where this part is give me a red underline in repl.it. Also I want it to take inputs of yes and no when I typed it in, but if the user enters something else like numbers or just random text just to loop until the user enters a yes or no.
    else
      raise ValueError
Reply
#4
Nevermind I fixed the assignment and it now works perfectly! :D
Reply
#5
Hi there! Congrats on finishing your clothing recommendation app. It sounds like you're facing a challenge with the rain-related code and the input validation. Don't worry, we've got you covered! Let's tackle it together.

To make sure the user enters a valid input for the rain and presence values, we can implement a loop that keeps asking for input until a valid value is provided. Here's an example of how you can achieve this:

while True:
try:
rain = float(input("How much rain is outside today? "))
presence = float(input("How much is present today? "))
break # Break out of the loop if both inputs are valid
except ValueError:
print("Invalid input. Please enter a valid number.")

# Continue with the rest of your code using the rain and presence variables
# ...
buran write Jun-22-2023, 10:50 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
buran write Jun-22-2023, 10:47 AM:
link removed
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020