Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python if语句每次都采用相同的路径;可能的strv。int冲突_Python_If Statement - Fatal编程技术网

Python if语句每次都采用相同的路径;可能的strv。int冲突

Python if语句每次都采用相同的路径;可能的strv。int冲突,python,if-statement,Python,If Statement,我正在开发一个程序,要求用户从两个洞穴中选择一个进入。用户可以选择洞穴1或洞穴2。将该数字与答案(由random.randint(1,2)生成)进行比较。如果用户的选择与答案相等,则他获胜;否则,他输了。问题是,该计划从未分支到win条件。无论用户做出什么选择,他总是输。我尝试过调试,但看不到caveAnswer和caveChoice之间的变量比较值 def gameCore (name): print ('You stand before the entrances of two ca

我正在开发一个程序,要求用户从两个洞穴中选择一个进入。用户可以选择洞穴1或洞穴2。将该数字与答案(由random.randint(1,2)生成)进行比较。如果用户的选择与答案相等,则他获胜;否则,他输了。问题是,该计划从未分支到win条件。无论用户做出什么选择,他总是输。我尝试过调试,但看不到caveAnswer和caveChoice之间的变量比较值

def gameCore (name):
    print ('You stand before the entrances of two caves.')
    print ('Choose a cave, either cave 1 or cave 2.')
    print ( )


    caveAnswer = random.randint (1,2)
    caveChoice = input ('Enter either 1 or 2. ')


    if caveAnswer == caveChoice:  [# I suspect the problem occurs at this comparison]
        print ('You enter the black mouth of the cave and...')
        time.sleep (1)
        print ( )
        print ('You find a hill of shining gold coins!')
        playAgain (name)

    else:
        print ('You enter the black mouth of the cave and...')
        time.sleep(1)
        print ( ) 
        print ('A wave of yellow-orange fire envelopes you. You\'re toast.')
        playAgain (name)
谢谢你的帮助

caveChoice = int(input ('Enter either 1 or 2. '))
您还应该使它在不是int时重试

您还应该使它在不是int时重试。

尝试转换为int

caveChoice=input('输入1或2')

尝试转换为int


caveChoice=input('输入1或2')

您应该将输入转换为int:

caveChoice = int(input('Enter either 1 or 2. '))
但是,如果您不希望程序在输入时崩溃,例如,
'foo'
,那么您需要一个
try except
块,它本身位于
while
循环中,这样您就可以再试一次

while True:
    try:
        caveChoice = int(input('Enter either 1 or 2. '))
        break
    except ValueError:
        print('Try again.')
此外,您可能需要检查输入是否实际为
1
2

while True:
    try:
        caveChoice = int(input('Enter either 1 or 2. '))
        if caveChoice not in (1, 2):
            raise ValueError
        break
    except ValueError:
        print('Invalid input. Try again.')

您应该将输入转换为int:

caveChoice = int(input('Enter either 1 or 2. '))
但是,如果您不希望程序在输入时崩溃,例如,
'foo'
,那么您需要一个
try except
块,它本身位于
while
循环中,这样您就可以再试一次

while True:
    try:
        caveChoice = int(input('Enter either 1 or 2. '))
        break
    except ValueError:
        print('Try again.')
此外,您可能需要检查输入是否实际为
1
2

while True:
    try:
        caveChoice = int(input('Enter either 1 or 2. '))
        if caveChoice not in (1, 2):
            raise ValueError
        break
    except ValueError:
        print('Invalid input. Try again.')

我试过你的程序,它确实有效。要测试它,只需在输入输入之前打印出
caveAnswer
。如果有错误,则不在此函数中

import random,time
def gameCore (name):
    print ('You stand before the entrances of two caves.')
    print ('Choose a cave, either cave 1 or cave 2.')
    print ( )


    caveAnswer = random.randint (1,2)
    print caveAnswer
    caveChoice = input ('Enter either 1 or 2. ')

    if caveAnswer == caveChoice:  
        print 'You enter the black mouth of the cave and - answer=%d - your answer=%d' % (caveAnswer, caveChoice)
        time.sleep (1)
        print ( )
        print 'You find a hill of shining gold coins!'
        # playAgain (name) 

    else:  
        print ('You enter the black mouth of the cave and - answer=%d - your answer=%d')% (caveAnswer, caveChoice)
        time.sleep(1)
        print ( ) 
        print ('A wave of yellow-orange fire envelopes you. You\'re toast.')
        # playAgain (name)

gameCore('Test')

我试过你的程序,它确实有效。要测试它,只需在输入输入之前打印出
caveAnswer
。如果有错误,则不在此函数中

import random,time
def gameCore (name):
    print ('You stand before the entrances of two caves.')
    print ('Choose a cave, either cave 1 or cave 2.')
    print ( )


    caveAnswer = random.randint (1,2)
    print caveAnswer
    caveChoice = input ('Enter either 1 or 2. ')

    if caveAnswer == caveChoice:  
        print 'You enter the black mouth of the cave and - answer=%d - your answer=%d' % (caveAnswer, caveChoice)
        time.sleep (1)
        print ( )
        print 'You find a hill of shining gold coins!'
        # playAgain (name) 

    else:  
        print ('You enter the black mouth of the cave and - answer=%d - your answer=%d')% (caveAnswer, caveChoice)
        time.sleep(1)
        print ( ) 
        print ('A wave of yellow-orange fire envelopes you. You\'re toast.')
        # playAgain (name)

gameCore('Test')


耶。现在,确认REPL中的假设:
1==“1”
。那么,怎样才能使双方“兼容”?(另请参见:“否则[如果不是数字],不同类型的对象[…]比较不相等。”)作为进一步的提示。试试
print((键入(caveAnswer),键入(caveChoice))
Arg,我之前评论中的旧链接(我责备谷歌“感觉幸运”)。2.x的当前版本是(这个版本有更好的格式,还讨论了协议行为)@pst:很明显OP使用的是3.x,否则,
input
会返回数字
1
,而不是字符串
“1”
,他一开始就不会有这个问题。(另外,
print()
将打印“()”而不是空行等)因此,如果要更新链接,您可能应该给出@Fluxcapacitor:一些小注释。首先,你通常不想逃避撇号和引号;你可以使用,例如,
“一股黄橙色的火焰包围着你。你真是太棒了。”
其次,不要在函数名和括号之间留空格,或者在括号内用
打印
,这可能是许多回答者没有意识到你在使用Python 3.Yay的部分原因。现在,确认REPL中的假设:
1==“1”
。那么,怎样才能使双方“兼容”?(另请参见:“否则[如果不是数字],不同类型的对象[…]比较不相等。”)作为进一步的提示。试试
print((键入(caveAnswer),键入(caveChoice))
Arg,我之前评论中的旧链接(我责备谷歌“感觉幸运”)。2.x的当前版本是(这个版本有更好的格式,还讨论了协议行为)@pst:很明显OP使用的是3.x,否则,
input
会返回数字
1
,而不是字符串
“1”
,他一开始就不会有这个问题。(另外,
print()
将打印“()”而不是空行等)因此,如果要更新链接,您可能应该给出@Fluxcapacitor:一些小注释。首先,你通常不想逃避撇号和引号;你可以使用,例如,
“一股黄橙色的火焰包围着你。你太棒了。”
其次,不要在函数名和括号之间留空格,或者在括号内用
print
这样做可能是许多回答者没有意识到你在使用Python 3的部分原因。是的,当然!我的意思是复制粘贴它并运行两三次。我认为Python2支持int/string比较,但Python3不支持。OP的问题在Python3中,因为
print
是一个函数。@Volatility
1==“1”
在Python2.x和Python3中为false。xI已经运行了10次,但从未分支到金币。我被难住了。我的逻辑看起来很可靠。我忽略了python的一些规则。哦,是的,没错。你们把
input
raw\u input
搞混了。我不知道为什么我的答案有效,而他最初的问题也不起作用。是的,当然!我的意思是复制粘贴它并运行两三次。我认为Python2支持int/string比较,但Python3不支持。OP的问题在Python3中,因为
print
是一个函数。@Volatility
1==“1”
在Python2.x和Python3中为false。xI已经运行了10次,但从未分支到金币。我被难住了。我的逻辑看起来很可靠。我忽略了python的一些规则。哦,是的,没错。你们把
input
raw\u input
搞混了。我不知道为什么我的答案有效,而他原来的问题不起作用。这解决了我的问题-谢谢这解决了我的问题-谢谢这是我说的“如果不是int,它会再试一次”。这是我说的“如果不是int,它会再试一次”。这和你说的不一样。事实上,它和