Programming exercise: What to wear tomorrow

Question:

I’m stuck on this exercise:

Please write a program that asks for tomorrow’s weather forecast and then suggests weather-appropriate clothing.

The suggestion should change if the temperature (measured in degrees Celsius) is over 20, 10, or 5 degrees, and also if there is rain on the radar.

Some examples of expected behavior:

What is the weather forecast for tomorrow?
Temperature: 21
Will it rain (yes/no): no
Wear jeans and a T-shirt
What is the weather forecast for tomorrow?
Temperature: 11
Will it rain (yes/no): no
Wear jeans and a T-shirt
I recommend a jumper as well
What is the weather forecast for tomorrow?
Temperature: 7
Will it rain (yes/no): no
Wear jeans and a T-shirt
I recommend a jumper as well
Take a jacket with you
What is the weather forecast for tomorrow?
Temperature: 3
Will it rain (yes/no): yes
Wear jeans and a T-shirt
I recommend a jumper as well
Take a jacket with you
Make it a warm coat, actually
I think gloves are in order
Don't forget your umbrella!

My code is:

temperature = int(input("Temperature: "))
rain = input("Will it rain (yes/no): ")

if temperature > 20:
    print("Wear jeans and a T-shirt")

if temperature > 10:
    print("I recommend a jumper as well")

if temperature > 5:
    print("Take a jacket with you")
 
if rain == "yes":
    print("Don't forget your umbrella!")

I don’t know how to resolve it! Can anyone teach me?

Asked By: Fiorent.dev

||

Answers:

This is my solutions hope it helps

def my_function(temperature):
  if temperature == "rain":
    return print("Don't forget your umbrella!")
    
  if temperature > 20:
    return print("Wear jeans and a T-shirt")

  if temperature > 10 and temperature < 20:
    return print("I recommend a jumper as well")

  if temperature > 5 and temperature < 10:
    return print("Take a jacket with you")
 
  
my_function(9)
my_function('rain')

open this to see in replit

Answered By: rizaldodo

From what I understand from your question, you need to output different messages depending on both the temperature and the rain status (whether it is raining).

You can try the below code I have created. Basically, the if statements above the indented blocks will check if there is rain or not. After checking, we will check the temperature. Depending on the temperature, we will output a certain message. Hope this helps.

temperature = int(input("Temperature: "))
rain = input("Will it rain (yes/no): ")

if rain == 'no':
    if temperature > 20:
        print('Wear jeans and a T-shirt')
    elif temperature > 10:
        print('Wear jeans and a T-shirt along with a jumper')
    else:
        print('Wear jeans, a T-shirt, a jumper and a jacket')

if rain == 'yes':
    if temperature > 20:
        print('Wear jeans and a T-shirt, don't forget your umbrella!')
    elif temperature > 10:
        print('Wear jeans and a T-shirt along with a warm coat, don't forget your umbrella!')
    else:
        print('Wear jeans, a T-shirt along with a warm coat. Gloves are in order as well. Don't forget your umbrella!')
Answered By: quik214

After reading through your question again, this solution might be a better help. This is a very simple way of solving the question.

temperature = int(input("Temperature: "))
rain = input("Will it rain (yes/no): ")

if temperature > 20:
    print('Wear jeans and a T-shirt')

if temperature > 10 and temperature <= 20:
    print('Wear jeans and a T-shirt')
    print('I recommend a jumper as well')

if temperature > 5 and temperature <= 10:
    print('Wear jeans and a T-shirt')
    print('I recommend a jumper as well')
    print('Take a jacket with you')


if temperature <= 5:
    print('Wear jeans and a T-shirt')
    print('I recommend a jumper as well')
    print('Take a jacket with you')
    print('Make it a warm coat, actually')
    print('I think gloves are in order')

if rain == 'yes':
    print('Don't forget your umbrella')
Answered By: quik214

I was attempting to answer this question. It was a little difficult for me, but after an hour of careful thought, I was able to figure it out. This is the link for my solution

https://tmc.mooc.fi/paste/kAUytfNVGhW0GSN6Gfsk4w

Answered By: golden_ratio
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.