Python Forum
If, elif, and else statement not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If, elif, and else statement not working
#1
Question 
#Whenever I put something in for the input it always says "That's Good!"
import time
user_action = "user"

print ("wassup")
time.sleep (0.5)
print ("My name is JoeChat. I am a chatbot made by PickleScripts.")
time.sleep (1)
user = input ("How are you doing?")
user = possible_actions = ("good", " good", "well", " well", "bad", " bad", "not good", " not good")

if user == "good" or " good" or "well" or " well": 
  print ("That's good!")
elif user == "bad" or " bad" or "not good" or " not good":
  print ("That's sad. :( I hope you feel better")
else:
  print ("I do not understand")
When I try to put "bad" in the output during the input part, it always says "That's good!"
Gribouillis write Mar-30-2023, 02:41 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
There are several errors in this code:
  • At line 9, the user = ... statement redefines the 'user' variable that contains the user's reply read at line 8.
  • At line 11, the == operator binds its operands more tightly than or, it means that the expression is equivalent to
if (user == "good") or " good" or "well" or " well": 
This is not what you intended. You could write
if user in ["good", " good", "well", " well"]:
Also note that the expression "good" or " good" or "well" or " well" is the same as "good" for Python, because in a series of or, the value of the whole expression is the first operand which boolean value is not False. If there is no such operand, the value of the whole expression is the last operand.
Reply
#3
My full output is,

wassup
my name is JoeChat. I am a chatbot made by PickleScripts.
How are you doing? bad
That's good!


after "How are you doing?" is the input part
Reply
#4
Thank You! It works now!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If statement not working correctly? MrKnd94 2 844 Nov-16-2022, 02:49 AM
Last Post: deanhystad
  If Statement not working...Why? Milfredo 2 2,217 Oct-17-2020, 03:23 AM
Last Post: Milfredo
  If elif else statement not functioning SamDiggityDog 4 2,659 Jun-03-2020, 12:27 AM
Last Post: SamDiggityDog
  Proper use of if..elif..else statement nick1941 2 2,430 Mar-06-2020, 11:22 PM
Last Post: nick1941
  Syntax Error (elif statement) Kanashi 0 3,680 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,259 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Invoking function in if else statement, not working! ibaad1406 13 5,664 May-30-2019, 09:05 PM
Last Post: ibaad1406
  Problem with elif statement Haddal99 2 2,288 May-20-2019, 09:26 AM
Last Post: avorane
  if-elif-else statement not executing laila1a 4 3,045 Jan-05-2019, 02:22 PM
Last Post: buran
  If statement not working oldcity 4 3,113 Oct-14-2018, 10:45 AM
Last Post: oldcity

Forum Jump:

User Panel Messages

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