Skip to content
Advertisement

Python program that tells you whether or not you need an umbrella when you leave the house [closed]

The program should:

  1. Ask you if it is raining using input()
  2. If the input is ‘y’, it should output ‘Take an umbrella’
  3. If the input is ‘n’, it should output ‘You don’t need an umbrella’

Advertisement

Answer

you can do this in several ways:

if input('it is raining now? y/n ') == 'y':
    print('Take an umbrella')
else:
    print("You don't need an umbrella")

also you can do it in just one line like this which is probabely an advanced way:

print('Take an umbrella' if (input('it is raining now? y/n: ') == 'y') else "You don't need an umbrella")

also you can first save the input in a variable, and then handle it in if blocks

x = input('it is raining now? y/n ')
if x.lower() == 'y':
    print('Take an umbrella')
elif x.lower() == 'n':
    print("You don't need an umbrella")
else:
    print('wrong input')

the x.lower() makes it acceptable even if user inputs Y or N in capital letters.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement