python while循环if_Python在while循环中忽略if语句

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值