我是python的新手,我无法弄清楚这个问题,我为字符串设置了if和elif,因此,如果他们的“心情”不好/不好/不好,它将执行一组指令,但是那没发生。我试图找到问题,但没有。代码如下:
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? ")
如果我为该语句输入好,它将执行我应该为坏语句得到的结果,如果我输入为错误,则表明该语句应该得到好处。
答案 0 :(得分:7)
find()
方法不返回布尔值。它返回找到句子的索引(如果找不到,则返回-1),因此,如果您说“ good”,它将在句子的索引0处看到它,但是由于条件需要布尔值,所以0被解释为False。 / p>
您可能想尝试一下:
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.")
答案 1 :(得分:0)
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? ")