python代码无法正常工作

时间:2014-05-20 22:05:51

标签: python python-2.7

我有一个石头剪刀游戏的代码:

import random
def rps():  
   computerchoice = random.randint(1,3) 
   if computerchoice == 1: 
     computerchoice = "rock" 
   elif computerchoice == 2: 
     computerchoice = "paper" 
   elif computerchoice == 3: 
     computerchoice = "scissors"
   choice = raw_input("Rock, paper, or scissors?:") 
   choice = choice.lower() 
   if choice != "rock":
     if choice != "paper":
       if choice != "scissors":
         print "Check your spelling and try again." 
         rps()
       else:
         pass
     else:
       pass 
   else: 
     print "The computer chose " + computerchoice + "." 
     if choice == computerchoice: 
       print "It's a draw!" 
     elif choice + computerchoice == "rockpaper": 
       print "Computer wins, paper covers rock!" 
     elif choice + computerchoice == "rockscissors": 
       print "Player wins, rock crushes scissors!" 
     elif choice + computerchoice == "paperrock": 
       print "Player wins, paper covers rock!" 
     elif choice + computerchoice == "paperscissors": 
       print "Computer wins, scissors cut paper!" 
     elif choice + computerchoice == "scissorsrock":
       print "Computer wins, rock crushes scissors!"
     elif choice + computerchoice == "scissorspaper":
       print "Player wins, scissors cuts paper!" 
rps()

每当我运行它时,如果我选择摇滚,它可以正常工作,但如果我选择纸张或剪刀,代码就会停止。它不会抛出任何错误,它只是停止。请帮帮我!

7 个答案:

答案 0 :(得分:1)

这应该更接近你的需要:

import random
def rps():
    computerchoice = random.randint(1,3)
    if computerchoice == 1:
        computerchoice = "rock"
    elif computerchoice == 2:
        computerchoice = "paper"
    elif computerchoice == 3:
        computerchoice = "scissors"
    choice = raw_input("Rock, paper, or scissors?:")
    choice = choice.lower()
    if choice not in ["scissors","paper","rock"]: # check if choice is valid
        rps()
    print "The computer chose " + computerchoice + "."
    if choice == computerchoice:   # move on to your comparison checks 
        choice + computerchoice
        print "It's a draw!"
    elif choice + computerchoice == "rockpaper":
        print "Computer wins, paper covers rock!"
    elif choice + computerchoice == "rockscissors":
        print "Player wins, rock crushes scissors!"
    elif choice + computerchoice == "paperrock":
        print "Player wins, paper covers rock!"
    elif choice + computerchoice == "paperscissors":
        print "Computer wins, scissors cut paper!"
    elif choice + computerchoice == "scissorsrock":
        print "Computer wins, rock crushes scissors!"
    elif choice + computerchoice == "scissorspaper":
        print "Player wins, scissors cuts paper!"
rps()

答案 1 :(得分:1)

问题来自第一个选择if语句:

    if choice != "rock":
        if choice != "paper":
            if choice != "scissors":

当选择rock时,它跳转到else语句而不评估其他两个if语句。更直观但不可否认的非Pythonic方法是使用一系列嵌套的if语句:

import random
def rps():  
    computerchoice = random.randint(1,3) 
    if computerchoice == 1: 
        computerchoice = "rock" 
    elif computerchoice == 2: 
        computerchoice = "paper" 
    elif computerchoice == 3: 
        computerchoice = "scissors"
    choice = raw_input("Rock, paper, or scissors?:") 
    choice = choice.lower() 
    print "The computer chose " + computerchoice + "." 
    if choice == 'rock':
        if computerchoice == 'rock':
            print 'Draw: you both picked rock'
        elif computerchoice == 'scissors':
            print 'You win! Rock beats scissors'
        elif computerchoice == 'paper':
            print 'You lose.  Try again'
    elif choice == 'paper':
        if computerchoice == 'rock':
            print 'You win!'
        elif computerchoice == 'scissors':
            print 'You lose.'
        elif computerchoice == 'paper':
            print 'You draw.'
    elif choice == 'scissors':
        if computerchoice == 'rock':
            print 'You lose.'
        elif computerchoice == 'scissors':
            print 'You draw.'
        elif computerchoice == 'paper':
            print 'You win.'
    else:
        print 'I am sorry, I could not make out what you typed.  Try again'
        rps()

rps()

答案 2 :(得分:1)

循环输入。不要递归地再次调用整个游戏。还设置变量以测试有效选择。此外,如果您打破胜利条件,您可以轻松添加。也许是这样的

import random
CHOICES = {'rock': 'crushes', 'paper': 'covers', 'scissors': 'cuts'}


def win(p1, p2):
    if p1 == p2:
        return 0
    if p1 == 'rock':
        return 2 if p2 == 'paper' else 1
    if p1 == 'paper':
        return 2 if p2 == 'scissors' else 1
    if p1 == 'scissors':
        return 2 if p2 == 'rock' else 1


def rps():
   computerchoice = random.choice(CHOICES.keys())
   choice = raw_input("Rock, paper, or scissors?:").lower()
   while choice not in CHOICES:
       print "Check your spelling and try again."
       choice = raw_input("Rock, paper, or scissors?:").lower()
   print "The computer chose %s." % computerchoice
   winner = win(choice, computerchoice)
   if winner==0:
       print "It's a draw!"
   if winner==1:
       print "Player wins, %s %s %s!" % (choice, CHOICES[choice], computerchoice)
   if winner==2:
       print "Computer wins, %s %s %s!" % (computerchoice, CHOICES[computerchoice], choice)


rps()

现在说您要添加lizardspock。只需更新CHOICESwin()功能即可。 :)

答案 3 :(得分:0)

检查这些行:

 if choice != "paper":
   if choice != "scissors":
     print "Check your spelling and try again." 
     rps()
   else:
     pass #here you tell it to do nothing when you choose "scissors"
 else:
   pass #here you tell it to do nothing when you choose "paper"

我认为您只需要对if / else语句进行重新排序: - )

答案 4 :(得分:0)

第一个else的{​​{1}}部分(包含代码的大脑):

if

如果玩家没有选择'摇滚' ' &# 39;剪刀' 以及任何无效输入将确保包含代码重要部分的if choice != "rock": if choice != "paper": if choice != "scissors": 无法执行。

答案 5 :(得分:0)

你绝对不需要递归这么简单的算法(从rps()调用rps()

你应该尝试像这样实现它:

input == ""
while input != "quit":
  input = raw_input(...)
  if input not in ["r", "p", "s"]: continue
  computer_choice = ...
  if foo beats bar: print ...
  if bar beats foo: print ...

答案 6 :(得分:0)

让我们来看看。

if choice != "rock":
  if choice != "paper":
    if choice != "scissors":
      print "Check your spelling and try again." 
      rps()
    else:
      pass
  else:
    pass 
else: 
  # Do stuff...

你输入剪刀。

  • "剪刀" !="摇滚"?是的,所以我们继续吧。

  • "剪刀" !="纸张#34;?是的,所以我们继续吧。

  • "剪刀" !="剪刀"?不,所以让我们看看替代的其他条款:

    else:
      pass
    

此代码不执行任何操作......此后,每个其他if/else子句都会被删除...请参阅问题?

您还可以进行大量简化,例如,您可以更频繁地使用表格和列表,而不是使用这么多if statements。例如(尽管代码仍然未经测试):

import random

def convert_choice(choice):
    choice_map = {"rock":0, "paper":1, "scissors":2}
    return choice_map[choice]

def rock_paper_scissors():
    choices = ["rock", "paper", "scissors"]
    computer_choice = random.randint(0,2)
    player_choice = raw_input("Rock, paper, or scissors? ") 
    raw_player_choice = convert_choice(player_choice) * 3
    print "Copmuter played: " + choices[computer_choice]
    print "You played: " + player_choice
    win_states = {0:"tied", 3:"won", 6:"loss", 
                  -1:"loss", 2:"tied", 5:"won",
                  -2:"won", 1:"loss", 4:"tied"}
    print "You " + win_states[raw_player_choice - computer_choice]

def main():
    rock_paper_scissors()

if __name__ == "__main__":
    main()