Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
elif vs. if
#3
response = input('Enter Yes No or Maybe')
if response == "Yes":
    print('Alright then')
elif response == "No":
    print('OK')
else
    print('Whenever you decide')

if response == "Yes":
    print('Alright then')
if response == "No":
    print('OK')
if response == "Maybe":
    print('Whatever you like')
These may look similar, but do have different behavior. In the first case, responds as you expect to Yes and No, and everything else defaults to the else which is Maybe. In the second case, it responds to Yes, No, and Maybe but will not respond to anything else, like 'Coconut'. The first example will treat Coconut as Maybe. So, it all depends on your particular switching needs.
Reply


Messages In This Thread
elif vs. if - by mortarm - Feb-10-2020, 03:32 PM
RE: elif vs. if - by DeaD_EyE - Feb-10-2020, 03:46 PM
RE: elif vs. if - by jefsummers - Feb-10-2020, 09:07 PM
RE: elif vs. if - by mortarm - Feb-18-2020, 04:30 PM
RE: elif vs. if - by DeaD_EyE - Feb-18-2020, 06:31 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020