The program should:
- Ask you if it is raining using input()
- If the input is ‘y’, it should output ‘Take an umbrella’
- If the input is ‘n’, it should output ‘You don’t need an umbrella’
Advertisement
Answer
you can do this in several ways:
JavaScript
x
5
1
if input('it is raining now? y/n ') == 'y':
2
print('Take an umbrella')
3
else:
4
print("You don't need an umbrella")
5
also you can do it in just one line like this which is probabely an advanced way:
JavaScript
1
2
1
print('Take an umbrella' if (input('it is raining now? y/n: ') == 'y') else "You don't need an umbrella")
2
also you can first save the input in a variable, and then handle it in if blocks
JavaScript
1
8
1
x = input('it is raining now? y/n ')
2
if x.lower() == 'y':
3
print('Take an umbrella')
4
elif x.lower() == 'n':
5
print("You don't need an umbrella")
6
else:
7
print('wrong input')
8
the x.lower() makes it acceptable even if user inputs Y or N in capital letters.