-2

How can i make this variable work withing the if statement in python?

arrived = input('Have the doorbell ringed?: ')

if arrived == 'yes' or 'Yes':
    doorbell = input('Is it from ifood?: ')
elif arrived == 'no' or 'No':
    print('It is not here yet, keep waiting')
else:
    print('Please answer the first question with yes or no')

if doorbell == 'yes' or 'Yes':
    print('Your ifood have arrived!!!')
elif doorbell == 'no' or 'No':
    print('It is not your ifood yet, keep waiting')
else:
    print('Please answer the questions with yes or no')

I'm a beginner and this started as an exercice, i've been searching this for a while and i know variables are global in python but is there a way to make it work?

2 Answers2

0

Your if is wrong. Better is:

  if arrived == 'yes' or arrived == 'Yes':

or

  if arrived in ['yes','Yes']:
Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39
0

First, your if statement is wrong - it needs to include a second arrived == or it won't work. It should look like this.

if arrived == 'yes' or arrived == 'Yes':

Second, you probably want to chuck the second if-elif-else inside the first if statement, since it should only trigger if that if statement condition is fulfilled.

Full solution:

arrived = input('Have the doorbell ringed?: ')

if arrived == 'yes' or arrived == 'Yes':
    doorbell = input('Is it from ifood?: ')
    if doorbell == 'yes' or 'Yes':
        print('Your ifood have arrived!!!')
    elif doorbell == 'no' or 'No':
        print('It is not your ifood yet, keep waiting')
    else:
        print('Please answer the questions with yes or no')
elif arrived == 'no' or arrived == 'No':
    print('It is not here yet, keep waiting')
else:
    print('Please answer the first question with yes or no')
Redz
  • 324
  • 1
  • 4
  • 16
  • Or do `arrived = input('Have the doorbell ringed?: ').lower()` and just ask for `if arrived == 'yes':`. Now you can input "Yes" or "yes" or "YES" or "yeS" ... – Matthias May 28 '23 at 17:18