0

当我输入 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!"

输出: 欢迎来到石头剪刀布!
一场胜利需要多少分?4
选择 (R)ock、(P)aper 还是 (s)cissors?r
人类:摇滚计算机:纸质
计算机获胜!
选择 (R)ock、(P)aper 还是 (s)cissors?r
Human:Rock Computer:Rock
A draw
选择 (R)ock、(P)aper 还是 (s)cissors?r
Human:Rock Computer:Rock
A draw
选择 (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

4

1 回答 1

1

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

于 2013-10-26T23:19:16.647 回答