【发布时间】:2015-05-08 01:40:08
【问题描述】:
我正在构建一个石头剪刀布游戏,我想在我已有的代码中添加一个计数器。
我想计算胜负和平局,并在玩家不想再玩时打印出来。
这段代码几乎完成了我打算做的所有其他事情。我在网站上看到了其他一些想法,但它们似乎都不适用于我的代码。有任何想法吗? 这是代码:
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()
【问题讨论】:
-
抱歉,澄清一下,我想计算胜负和平局,并在玩家不想再玩时打印出来。
-
你试过什么?你被困在哪里了?哪些想法看起来很有希望但不合适,为什么?
-
作为提示:您知道 (a) 从函数返回多个值,还是 (b) 全局变量?其中任何一个都应该使这变得非常容易。即使没有这些,您也只需要返回一个表示赢/输/领带的值,然后在
main中编写另一个 if/elif/elif 语句。
标签: python