首页 文章

Python在while循环中忽略if语句

提问于
浏览
0

我最近在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

    choose() 不返回值 . 因此它总是返回 None . None 永远不会等于 0 . 因此,永远不会触发 if 语句 .

    在Python 3中,比较 None0 实际上是一个错误,这将有助于您更快地解决这个问题 . 但是,即使在Python 2中,在您调用 choose() 之后,一个简陋的 print humanchoice 语句很快就会显示您从函数中得到的除了 None 之外没有任何东西 .

    choose() 末尾添加 return humanchoice .

  • 0

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

    humanchoice = choose()
    

相关问题