蟒蛇岩纸剪刀柜台

时间:2015-05-08 01:40:09

标签: python

我正在建造一个石头剪刀游戏,我想在我已有的代码中添加一个计数器。

我想计算胜利,失败和关系,并在玩家不想再玩时打印出来。

这段代码完成了我打算做的其他事情。我在网站上看到了其他一些想法,但它们似乎都不适合我的代码。有任何想法吗? 这是代码:

import random

def rock(rand):

    if rand == (1):
        print("Tie Game!")
    elif rand == (2):
        print("Your rock got covered by opponent's paper. You Lose!")
    elif rand == (3):
        print("You crushed the opponent's scissors with your rock! You Win!")
        print("Good game!")

def paper(rand):

    if rand == (1):
        print("You covered opponent's rock with your paper. You Win!")
        print("Good game!")
    elif rand == (2):
        print("Tie Game!")
    elif rand == (3):
        print("Your opponent cut your paper with its scissors. You Lose!")

def scissors(rand):

    if rand == (1):
        print("Your opponent's rock crushed your scissors. You Lose!")
    elif rand == (2):
        print("Your scissors cut opponent's paper. You Win!")
        print("Good game!")
    elif rand == (3):
        print("Tie Game!")

def main():

    name = input("What is your name? ")

    print("Hello, " + name + "! I'm a rock paper scissors game.")

    while True:
        choice = input("Would you like to play? (yes/no) ").upper()[0]
        if choice == "N":
            print("Let's play later!")
            break
        elif choice == "Y":
            print("Ok, lets play!")
            print("Rules: Rock breaks Scissors, Scissors cuts Paper, Paper covers Rock. ")
            raw = input("Choose rock, paper, or scissors ").upper()
            try:
                answer = raw[0]
            except:
                IndexError
                print("Illegal input! Exiting.")
                break


            rand = random.randint(1, 3)  # 1 = rock, 2 =Paper 3 = Scissors
            if answer == "S":
                scissors(rand)
            elif answer == "P":
                paper(rand)
            elif answer == "R":
                rock(rand)
            else:
                print("Enter a valid answer!")
main()

1 个答案:

答案 0 :(得分:1)

最好将所有方法放在一个类中而不是使用全局变量。 你走了。

import random

my_win = 0
my_loss = 0
my_tie = 0



def rock(rand):

    global my_tie, my_loss, my_win
    if rand == (1):
        print("Tie Game!")
        my_tie += 1
    elif rand == (2):
        print("Your rock got covered by opponent's paper. You Lose!")
        my_loss += 1
    elif rand == (3):
        print("You crushed the opponent's scissors with your rock! You Win!")
        print("Good game!")
        my_win += 1

def paper(rand):

    if rand == (1):
        print("You covered opponent's rock with your paper. You Win!")
        print("Good game!")
        my_win += 1
    elif rand == (2):
        print("Tie Game!")
        my_tie += 1
    elif rand == (3):
        print("Your opponent cut your paper with its scissors. You Lose!")
        my_loss += 1

def scissors(rand):

    if rand == (1):
        print("Your opponent's rock crushed your scissors. You Lose!")
        my_loss += 1
    elif rand == (2):
        print("Your scissors cut opponent's paper. You Win!")
        print("Good game!")
        my_win += 1
    elif rand == (3):
        print("Tie Game!")
        my_tie += 1

def main():

    name = input("What is your name? ")

    print("Hello, " + name + "! I'm a rock paper scissors game.")

    while True:
        choice = input("Would you like to play? (yes/no) ").upper()[0]
        if choice == "N":
            print("Let's play later!")
            break
        elif choice == "Y":
            print("Ok, lets play!")
            print("Rules: Rock breaks Scissors, Scissors cuts Paper, Paper covers Rock. ")
            raw = input("Choose rock, paper, or scissors ").upper()
            try:
                answer = raw[0]
            except:
                print("Illegal input! Exiting.")
                break


            rand = random.randint(1, 3)  # 1 = rock, 2 =Paper 3 = Scissors
            if answer == "S":
                scissors(rand)
            elif answer == "P":
                paper(rand)
            elif answer == "R":
                rock(rand)
            else:
                print("Enter a valid answer!")
    print ("You win %d times!" % my_win)
    print ("You lose %d times!" % my_loss)
    print ("You tie %d times!" % my_tie)
main()

输出:

What is your name? a
Hello, a! I'm a rock paper scissors game.
Would you like to play? (yes/no) y
Ok, lets play!
Rules: Rock breaks Scissors, Scissors cuts Paper, Paper covers Rock. 
Choose rock, paper, or scissors Rock
Tie Game!
Would you like to play? (yes/no) no
Let's play later!
You win 0 times!
You lose 0 times!
You tie 1 times!

Process finished with exit code 0