我正在构建一个石头剪刀游戏,我想在我已有的代码中添加一个计数器 .
我想计算胜利,失败和关系,并在玩家不想再玩时打印这些 .
这段代码完成了我打算做的其他事情 . 我在网站上看到了一些其他的想法,但它们似乎都不适合我的代码 . 有任何想法吗?这是代码:
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 回答
将所有方法放在类中而不是使用全局变量会更好 . 干得好 .
OUTPUT: