多条件While循环

2024-05-19 01:44:36 发布

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

我在做一个简单的猜谜游戏的过程中遇到了一些问题。首先,我是Python新手,这是我除了批处理之外的第一种编程语言。 当我尝试执行以下代码时,elifelse不会相互作用,即使条件为true也是如此。它似乎总是对if作出反应,好像它总是对的。你知道吗

#Calder Hutchins 3-1-14
#Guess Game
import time
playGame = ""
guessTry = 3
print "Would you like to play a game? (y/n)"
while playGame != "y":
    playGame = raw_input(">")
    if playGame == "y" or "n":
        print "Great!  Let's get started!"
        print "Answer the following questions correctly to level up!"
    elif playGame == "n":
        print "Very well...."
        time.sleep(2)
        quit()
    else:
        print "Invalid answer, please re-type."
print "end of the while, test."

我还想知道流控制,因为它是新的我。有没有办法让它更有效,我粘贴的代码。你知道吗

编辑:这不是重复的。。。如果你们中的一些人愿意花时间来认真研究我的问题,你会发现事实并非如此。我发现在堆栈溢出上多次出现这种情况非常令人失望。你知道吗


Tags: theto代码游戏iftime过程条件
1条回答
网友
1楼 · 发布于 2024-05-19 01:44:36

计算机是哑的,你需要明确地说明你想要评估什么。你知道吗

if playGame == "y" or playgame == "n":

在您的版本中,它总是求值为true,因为当比较两个字符串时,它的求值结果将为Truestrstr总是True

>>> bool('a' or 'c')
True

相关问题 更多 >

    热门问题