if/elif语句在while循环中不起作用

2024-04-20 02:03:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个简单的8ball响应程序,有一个问题。当我运行这个程序,但给出除“y”或“yes”以外的任何选项来响应变量“rd”时,程序认为我实际输入了“yes”,并继续使用“if”语句中缩进的代码来响应yes。为什么会这样?我不明白为什么。你知道吗

import time
import random
import sys


resp = ["Yes!", "No!", "Maybe!", "Don't be so silly!",
        "In your dreams!", "Without a doubt!", "Most likely!",
        "Very doubtful!", "I'm going to have to say no this time!",
        "What kind of a question is that? Of course not!", "I reckon there's a 20% chance!",
        "Better not tell you now", "I've been told by to tell you no...",
        "I've been told to tell you yes...", "It's possible!", "More likely to see pigs fly!",
        "You wish!", "All signs point to no!", "All signs point to yes!",
        "If you truly believe it!"
    ]

def intro():
    print "Hello! Welcome to 8 Ball!\n"
    time.sleep(2)

def main():
    quit = 0
    while quit != "n":
        rd = raw_input("Are you ready to play? Enter y/n: ")
        if rd.lower() == "y" or "yes":
            question = raw_input("\nType your question and please press enter: ")
            print "\n"
            print random.choice(resp)
            print "\n"
            quit = raw_input("Do you want to roll again? Enter y/n: ")
        elif rd.lower() == "n" or "no":
            print "Looks like you need some more time to think. Have a few seconds to think about it.."
            time.sleep(3)
            quit = raw_input("Are you ready to play now? Enter y/n: ")
        else:
            print "That wasn't an option. Try again."
            rd = raw_input("Are you ready to play? Enter y/n: ") 
    print "Okay! Thanks for playing."

intro()
main()

Tags: tonoimport程序youinputrawtime
3条回答

你不能这样做:

if rd.lower() == "y" or "yes":

因为它自己在评估“是”。相反,请尝试:

if rd.lower() == "y" or rd.lower() == "yes":

同时考虑:

if rd.lower() in ["y", "yes"]:
>>> bool("yes")
True

“是”的计算结果为真

if rd.lower() in ("y", "yes"):

可以用来检查值是'y'还是'yes'

在python中不能执行if x == a or b,必须执行x == a or x == bx in (a, b) 导入时间 随机导入 导入系统

resp = ["Yes!", "No!", "Maybe!", "Don't be so silly!",
        "In your dreams!", "Without a doubt!", "Most likely!",
        "Very doubtful!", "I'm going to have to say no this time!",
        "What kind of a question is that? Of course not!", "I reckon there's a 20% chance!",
        "Better not tell you now", "I've been told by to tell you no...",
        "I've been told to tell you yes...", "It's possible!", "More likely to see pigs fly!",
        "You wish!", "All signs point to no!", "All signs point to yes!",
        "If you truly believe it!"
    ]

def intro():
    print "Hello! Welcome to 8 Ball!\n"
    time.sleep(2)

def main():
    quit = 0
    while quit != "n":
        rd = raw_input("Are you ready to play? Enter y/n: ")
        if rd.lower() in ("y", "yes"):
            question = raw_input("\nType your question and please press enter: ")
            print "\n"
            print random.choice(resp)
            print "\n"
            quit = raw_input("Do you want to roll again? Enter y/n: ")
        elif rd.lower() in ("n", "no"):
            print "Looks like you need some more time to think. Have a few seconds to think about it.."
            time.sleep(3)
            quit = raw_input("Are you ready to play now? Enter y/n: ")
        else:
            print "That wasn't an option. Try again."
            rd = raw_input("Are you ready to play? Enter y/n: ") 
    print "Okay! Thanks for playing."

intro()
main()

相关问题 更多 >