gpt4 book ai didi

python石头剪刀布计数器

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:46 26 4
gpt4 key购买 nike

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

我想计算胜利、失败和平局,并在玩家不想再玩时打印这些。

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

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

最佳答案

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

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

关于python石头剪刀布计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com