djofox
djofox

Reputation: 31

Python if statements getting mixed up

Im a newbie to python and I cant figure out this problem out, I have set if and elif for a string, so if their 'mood' is bad/good/not good it will do a certain set of instructions, but thats not happening. I tried finding the problem but couldnt. Heres the code:

import time

name = input("Hello! Whats your name: ")

mood = input("Hello, " + name + " how are you feeling today? ")

if mood.find("good"):
  time.sleep(0.3)
  print("Thats awesome!")

elif mood.find("bad"):
  time.sleep(0.3)
  input("Oh, whats made you feel so down? ")
  print("Aww, thats ok.")

time.sleep(1)
activity = input("What are you doing today? ")

If I enter good for the statement, it runs what I should get for bad, if I enter bad it says what it should be for good.

Upvotes: 3

Views: 183

Answers (2)

linda
linda

Reputation: 1

import time
name = input("Hello! Whats your name: ")

mood = input("Hello, " + name + " how are you feeling today? ")

if "good" in mood:
  time.sleep(0.3)
  print("Thats awesome!")
if "bad" in mood:
  time.sleep(0.3)
  input("Oh, whats made you feel so down? ")
  print("Aww, thats ok.")

time.sleep(1)
activity = input("What are you doing today? ")

Upvotes: 0

Fukiyel
Fukiyel

Reputation: 1166

The find() method does not return a boolean. It returns the index at which it finds the sentence (-1 if not found), so if you say "good", it will see it at index 0 in your sentence, but because conditions want booleans, 0 is interpreted as False.

You might want to try this instead :

if "good" in mood:
    time.sleep(0.3)
    print("Thats awesome!")

elif "bad" in mood:
    time.sleep(0.3)
    input("Oh, whats made you feel so down? ")
    print("Aww, thats ok.")

Upvotes: 7

Related Questions