'Beginner Python program flow wrong somewhere
import random
bored = input('Are you bored? Please answer with a \'yes\' or \'no\'')
activities = ['Read a book', 'Watch a movie', 'Do some physical activities you enjoy', 'Play a card game', 'Bake something', 'Finish something you never fnished.']
if bored == 'no' or 'No':
print('Then what are you doing here?')
else:
if bored == 'yes' or 'Yes':
print(random.choice(activities))
this code don't do what it should.
can please somebody help me?
Solution 1:[1]
Try:
import random
bored = input("Are you bored? Please answer with a 'yes' or 'no': ")
activities = ['Read a book', 'Watch a movie', 'Do some physical activities you enjoy', 'Play a card game', 'Bake something', 'Finish something you never fnished.']
if bored == 'no' or bored == 'No':
print('Then what are you doing here?')
else:
print(random.choice(activities))
Issues
- Incorrect conditional
These conditional do not produce what you expect (i.e. always act as True)
if bored == 'no' or 'No':
if bored == 'yes' or 'Yes':
Use one of these three options:
if bored == 'no' or bored == 'No':
if bored.lower() == 'no':
if bored in ('No', 'no'):
- On booleans no need to check both values (i.e. 'no' and 'yes')
Simplify this:
if bored == 'no' or 'No': # incorrect conditional
print('Then what are you doing here?')
else:
if bored == 'yes' or 'Yes': # no need to check for 'yes'
print(random.choice(activities))
To:
if bored == 'no' or bored == 'No':
print('Then what are you doing here?')
else:
print(random.choice(activities))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |