Python在while循环中忽略if语句

时间:2016-07-12 05:41:05

标签: python python-2.7

我最近在Codecademy上学习了Python,所以我决定为我的第一个真实程序制作摇滚,纸张,剪刀游戏,因为它看起来很简单。

在测试程序时,Python似乎忽略了嵌套在while循环中的大if / elif语句。游戏生成一个随机整数(0为摇滚,1为纸,2为剪刀),然后打印(用于调试)。然后它会提示玩家输入。之后,不是用if语句评估选择,而是要求玩家做出另一种选择。它还打印一个新的随机整数,所以我知道它只是跳过if语句并返回到while循环的开头。

以下是游戏的代码。如果if语句出现某种语法错误,我就没有看到它。有谁知道发生了什么事?

from random import randint

def choose():
    print '\nWill you play rock, paper, or scissors?'
    rawhumanchoice = raw_input('> ')
    if rawhumanchoice == 'rock' or rawhumanchoice == 'r':
        humanchoice = 0
    elif rawhumanchoice == 'paper' or rawhumanchoice == 'p':
        humanchoice = 1
    elif rawhumanchoice == 'scissors' or rawhumanchoice == 's':
        humanchoice = 2
    else:
        print '\nSorry, I didn\'t catch that.'
        choose()

def gameinit():
    roundsleft = 0
    pcwins = 0
    humanwins = 0

    print 'How many rounds do you want to play?'
    roundsleft = raw_input('> ')

    while roundsleft > 0:
        pcchoice = randint(0,2)
        print pcchoice

        humanchoice = -1
        choose()

        if humanchoice == 0: #This is what Python ignores
            if pcchoice == 0:
                print '\nRock and rock... it\'s a tie!'
                roundsleft -= 1
            elif pcchoice == 1:
                print '\nPaper beats rock... PC wins.'
                roundsleft -= 1
                pcwins += 1
            elif pcchoice == 2:
                print '\nRock beats scissors... human wins!'
                roundsleft -= 1
                humanwins += 1
        elif humanchoice == 1:
            if pcchoice == 0:
                print '\nPaper beats rock... human wins!'
                roundsleft -= 1
                humanwins += 1
            elif pcchoice == 1:
                print '\nPaper and paper... it\'s a tie!'
                roundsleft -= 1
            elif pcchoice == 2:
                print '\nScissors beat paper... PC wins.'
                roundsleft -= 1
                pcwins += 1
        elif humanchoice == 2:
            if pcchoice == 0:
                print '\nRock beats scissors... PC wins.'
                roundsleft -= 1
                pcwins += 1
            elif pcchoice == 1:
                print '\nPaper beats rock... human wins!'
                roundsleft -= 1
                humanwins += 1
            elif pcchoice == 2:
                print '\nScissors and scissors... it\'s a tie!'
                roundsleft -= 1
    else:
        if humanwins > pcwins:
            result = 'The human wins the match!'
        elif humanwins < pcwins:
            result = 'The PC wins the match.'
        elif humanwins == pcwins:
            result = 'The match is a tie!'

        print '\nThe score is %s:%s... %s \n' % (humanwins,pcwins,result)
        gameinit()

gameinit()

2 个答案:

答案 0 :(得分:0)

你缺少从方法选择返回,并用

捕捉它
humanchoice = choose()

答案 1 :(得分:0)

choose()不会返回值。因此,它始终返回NoneNone永远不会等于0。因此,永远不会触发if语句。

在Python 3中,比较None0实际上会出错,这有助于您更快地解决这个问题。但是,即使在Python 2中,在致电print humanchoice之后发出一句简陋的choose()声明,很快就会发现除了你的None之外你还没有得到任何东西。

return humanchoice的末尾添加choose()

相关问题