Python Forum
Why does this elif_statement not work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does this elif_statement not work?
#1
Hi!

My code:
feeling = input('How are you?\n')
if feeling.lower() == 'great':
    print('I feel great too.')
elif feeling.lower == 'bad':
    print('I hope the rest of your day will be good.')
else:
    print('Take it easy!')

and the output (please focus on the second execution)

Output:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> RESTART: C:/Users/kr-ga/AppData/Local/Programs/Python/Python37-32/BOOK_automate_the/Chapter 6/test2.py How are you? great I feel great too. >>> RESTART: C:/Users/kr-ga/AppData/Local/Programs/Python/Python37-32/BOOK_automate_the/Chapter 6/test2.py How are you? bad Take it easy! >>>
My question is apparently, why does my program print 'take it easy', instead of 'I hope the rest of your day will be good.', when the input is 'bad'?
Can anyone please spot my error?
Reply
#2
You need parentheses after the lower on line 4. You're comparing the input to the method, not the result of calling the method.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
In line 4 you are not calling the lower method of a string. Instead you are comparing the string "bad" to the lower method. Instead you should use parentheses ( feeling.lower() ), like you did in line 2.
Reply
#4
Omg, lol,
I have looked yesterday more than 10 times over the code, but just have overlooked it completely.
Thanks for quickly clearing it up^^
Reply


Forum Jump:

User Panel Messages

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