蟒蛇岩纸剪刀

时间:2016-02-13 11:43:13

标签: python python-2.7

我是编程新手,并尝试在python 2.7中构建一个简单的石头剪刀程序。在我的函数中,我有2个主要的if语句

rules = raw_input("Before we being playing would you like to hear the rules first? ")
if rules.lower() == "yes":
    print """ 
    Scissors cuts Paper 
    Paper covers Rock 
    Rock crushes Scissors"""

和第二个

choice = raw_input("please enter your choice? (Must be either rock, paper or scissors) ")
computer = random.choice(["rock", "paper", "scissors"])

if choice == "rock" or "paper" or "scissors": 
    if choice == computer : 
        print "Its a tie !"
    elif choice == "rock" and computer == "scissors": 
        print "Rock beats scissors you win!."

    elif choice == "rock" and computer == "paper": 
        print "Paper beats rock you loose !."

    elif choice == "paper" and computer == "scissors": 
        print "Scissors beats paper you loose !."

    elif choice == "paper" and computer == "rock": 
        print "Paper beats rock you win !."

    elif choice == "scissors" and computer == "paper": 
        print "Scissors beats paper you win !."

    elif choice == "scissors" and computer == "rock": 
        print "Rock beats scissors you loose !."
else : 
    print "Invalid Entry Please try again."

单独的两位代码都可以正常工作,但是当我尝试将它们放在一个函数中时,第一个if语句询问规则是否有效,但随后在具有该程序主要功能的第二个if语句之前退出。我试过在第一个if语句中缩进第二位代码,但它似乎不起作用

我想知道是否还有使这两段代码在一个简单的函数中工作?或者我应该用这两个函数创建一个类? 如果有人有任何关于如何使我的程序更好的提示,请告诉我。在此先感谢您的帮助。

继承完整代码

import random 

def rock_paper_scissors_spock():

    rules = raw_input("Before we being playing would you like to hear the rules first? ")
    if rules.lower() == "yes":
        print """ 
        Scissors cuts Paper 
        Paper covers Rock 
        Rock crushes Scissors"""

    choice = raw_input("please enter your choice? (Must be either rock, paper or  scissors) ")
    computer = random.choice(["rock", "paper", "scissors"])

    if choice == "rock" or "paper" or "scissors": 
        if choice == computer : 
            print "Its a tie !":

        elif choice == "rock" and computer == "scissors": 
            print "Rock beats scissors you win!."

        elif choice == "rock" and computer == "paper": 
            print "Paper beats rock you loose !."

        elif choice == "paper" and computer == "scissors": 
            print "Scissors beats paper you loose !."

        elif choice == "paper" and computer == "rock": 
            print "Paper beats rock you win !."

        elif choice == "scissors" and computer == "paper": 
            print "Scissors beats paper you win !."

        elif choice == "scissors" and computer == "rock": 
            print "Rock beats scissors you loose !."
    else : 
        print "Invalid Entry PLease try again." 


rock_paper_scissors_spock()

1 个答案:

答案 0 :(得分:2)

您说if choice == "rock" or "paper" or "scissors":,但Python没有将choice ==连接到所有选项。你可以在choice == "rock"附近加上括号,它会做同样的事情。将其更改为if choice in ("rock", "paper", "scissors")

相关问题