纸,摇滚&剪刀游戏。(初级)

时间:2013-10-26 23:15:42

标签: python-2.7

当我输入4作为我的最大点数限制时,代码以5结束循环。我无法理解

import random

p=0
x = int(raw_input("How many points are required for a win? "))
while p<=x:
     y = raw_input("Choose (R)ock, (P)aper, or (s)cissors? ")
     z1 = ('Rock', 'Paper', 'Scissors')
     z = random.choice(z1)
     if y=='r':
          print "Human: Rock  Computer: " + z
          if z=='Rock':
               print "A draw"
          if z=='Paper':
               print "Computer wins!"
          if z=='Scissors':
               print "Human wins!"
               p +=1
               if p ==(x-1):
                   print "Only need one more point!"
               print "Your score is: " + str(p)

     elif y=='p':
          print "Human: Paper Computer: " + z
          if z=='Paper':
               print "A draw"
          if z=='Rock':
               print "Human wins!"
               p +=1
               if p==(x-1):
                   print "Only need one more point!"
               print "Your score is: " + str(p)
          if z=='Scissors':
               print "Computer wins!"

     elif y=='s':
          print "Human: Scissors Coputer: " + z
          if z=='Scissors':
               print "A draw"
          if z=='Paper':
               print "Human wins!"
               p +=1
               if p==(x-1):
                   print "Only need one more point!"
               print "Your score is: " + str(p)
          if z=='Rock':
               print "Computer wins!"

输出: 欢迎来到Rock,Paper,Scissors!
获胜需要多少分? 4
选择(R)ock,(P)aper还是(s)cissors? [R
人:摇滚电脑:纸
电脑赢了!
选择(R)ock,(P)aper还是(s)cissors? [R
人:摇滚电脑:摇滚 抽奖
选择(R)ock,(P)aper还是(s)cissors? [R
人:摇滚电脑:摇滚 抽奖
选择(R)ock,(P)aper还是(s)cissors? [R
人类:摇滚电脑:剪刀
人类获胜!
你的分数是:1
选择(R)ock,(P)aper还是(s)cissors? [R
人:摇滚电脑:纸
电脑赢了!
选择(R)ock,(P)aper还是(s)cissors? [R
人类:摇滚电脑:剪刀
人类获胜!
你的分数是:2
选择(R)ock,(P)aper还是(s)cissors? [R
人类:摇滚电脑:剪刀
人类获胜!
只需要再多一点!
你的分数是:3
选择(R)ock,(P)aper还是(s)cissors? [R
人类:摇滚电脑:剪刀
人类获胜!
你的分数是:4
选择(R)ock,(P)aper还是(s)cissors? [R
人类:摇滚电脑:剪刀
人类获胜!
你的分数是:5

1 个答案:

答案 0 :(得分:1)

您的while循环条件为p<=x,这意味着如果p等于x - 即,如果人类具有所需的点数 - 则循环将再次运行。将其更改为while p<x: