python代码不工作

python代码不工作,python,python-2.7,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: computerch

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

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()

无论何时我运行它,如果我选择石头,它都可以正常工作,但是如果我选择了纸或剪刀,代码就会停止。它不会抛出任何错误,它只是停止。请帮帮我

检查这些行:


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

第一个
if
else
部分(包含代码的大脑):

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

如果玩家没有选择“石头”即“布”、“剪刀”,则永远不要运行,任何无效的输入都将确保包含代码重要部分的
其他
不会执行。

对于这样一个简单的算法,您肯定不需要递归(从
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 ...
让我们看看这个

if choice != "rock":
  if choice != "paper":
    if choice != "scissors":
      print "Check your spelling and try again." 
      rps()
    else:
      pass
  else:
    pass 
else: 
  # Do stuff...
你可以输入剪刀

  • “剪刀”吗“摇滚”?是的,那么让我们更进一步

  • “剪刀”吗“纸”?是的,那么让我们更进一步

  • “剪刀”吗“剪刀”?不,让我们看一下替代else条款:

    else:
      pass
    
此代码不起任何作用。。。在此之后,每隔一个
if/else
子句就会被分解出来。。。看到问题了吗

您还可以进行许多简化,例如,您可以更频繁地使用表和列表,而不是使用如此多的
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()
    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()

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

    if choice != "rock":
        if choice != "paper":
            if choice != "scissors":
选择rock后,它将跳转到else语句,而不计算其他两个if语句。一种更直观但公认的非python方式是使用一系列嵌套的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()

循环你的输入。不要再重复调用整个游戏。还要设置一个变量来测试有效选项。此外,如果你打破了胜利的条件,你可以很容易地添加到它。也许是这样的

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()

现在假设您想添加
lizard
spock
。只需更新
选项
win()

elif choice+computerchoice==“rockpaper”
现在这一切都搞砸了。。。你不想用适当的比较吗<代码>如果a==“R”和b==“P”:
@Pavel我当时找不到办法。我只是觉得无聊,开始做这个,所以我只是尽可能简单(即使它确实占用了相当多的空间)
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()
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()