Python 巨蟒石剪纸计数器
我正在建立一个石头剪刀布游戏,我想添加一个计数器的代码,我已经有 我想数一数胜利、失败和平局,当玩家不想再玩的时候打印出来 这段代码几乎完成了我开始要做的所有事情。我在网站上看到了一些其他的想法,但似乎没有一个适合我的代码。有什么想法吗? 代码如下:Python 巨蟒石剪纸计数器,python,Python,我正在建立一个石头剪刀布游戏,我想添加一个计数器的代码,我已经有 我想数一数胜利、失败和平局,当玩家不想再玩的时候打印出来 这段代码几乎完成了我开始要做的所有事情。我在网站上看到了一些其他的想法,但似乎没有一个适合我的代码。有什么想法吗? 代码如下: import random def rock(rand): if rand == (1): print("Tie Game!") elif rand == (2): print("Your roc
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
抱歉,为了澄清,我想计算赢、输和平局,当玩家不想再玩时打印这些。你尝试过什么?你被困在哪里?什么想法看起来很有希望但不合适,为什么?作为提示:你知道(a)从函数返回多个值,或者(b)全局变量吗?这两种方法中的任何一种都会让这变得很容易。即使没有这些,您也只需返回一个表示win/loss/tie的值,然后在
main
中编写另一个if/elif/elif语句即可。我已经厌倦了一些类似的解决方案,但我没有在正确的位置使用全球的赢家、输家和平局。谢谢你的帮助!