我是python的新手(并为此编码)并正在进行摇滚,剪纸和剪刀练习。在第二场比赛之后我的代码不会问我是否想要打另一场比赛,而是自动调用游戏功能。有谁知道我哪里出错了?
import sys
print("Welcome to rock, paper, scissors!")
player_a = input("Player A input one of the following; rock, paper or scissors: ")
player_b = input("Player B input one of the following; rock, paper or scissors: ")
def game(p1, p2):
if p1 == p2:
print("It is a draw!")
elif p1 == "rock" and p2 == "scissors":
print("Player A wins!")
elif p1 == "rock" and p2 == "paper":
print("Player B wins!")
elif p1 == "paper" and p2 == "rock":
print("Player A wins!")
elif p1 == "paper" and p2 == "scissors":
print("Player B wins!")
elif p1 == "scissors" and p2 == "rock":
print("Player A wins!")
elif p1 == "scissors" and p2 == "paper":
print("Player B wins!")
game(player_a, player_b)
playAgain = input("Would you like to play again? (Insert Y or N): ")
while playAgain == "Y":
player_a = input("Player A input one of the following; rock, paper or scissors: ")
player_b = input("Player B input one of the following; rock, paper or scissors: ")
game(player_a, player_b)
else:
print("Thanks for playing!")
sys.exit()
答案 0 :(得分:5)
你只需要再次播放"再玩一次?"你的循环中的问题。
编码的一个重要原则是DRY:Don't Repeat Yourself。通过一点点重组,我们可以摆脱代码中的一些重复。
在下面的代码中,我们假装在程序开始时用户已经回答了" Y"再次播放?"问题
print("Welcome to rock, paper, scissors!")
def game(p1, p2):
if p1 == p2:
print("It is a draw!")
elif p1 == "rock" and p2 == "scissors":
print("Player A wins!")
elif p1 == "rock" and p2 == "paper":
print("Player B wins!")
elif p1 == "paper" and p2 == "rock":
print("Player A wins!")
elif p1 == "paper" and p2 == "scissors":
print("Player B wins!")
elif p1 == "scissors" and p2 == "rock":
print("Player A wins!")
elif p1 == "scissors" and p2 == "paper":
print("Player B wins!")
playAgain = "Y"
while playAgain == "Y":
player_a = input("Player A input one of the following; rock, paper or scissors: ")
player_b = input("Player B input one of the following; rock, paper or scissors: ")
game(player_a, player_b)
playAgain = input("Would you like to play again? (Insert Y or N): ")
else:
print("Thanks for playing!")
请注意,无需拨打sys.exit()
,我们可以让程序自然终止。
我们还可以大大压缩game
函数。如果它不是平局,并且A没有赢,那么B 必须获胜。所以我们不需要做所有那些B测试。我们可以使用or
运算符将A测试合并到一个测试中。
def game(p1, p2):
if p1 == p2:
print("It is a draw!")
elif ((p1 == "rock" and p2 == "scissors")
or (p1 == "paper" and p2 == "rock")
or (p1 == "scissors" and p2 == "paper")):
print("Player A wins!")
else:
print("Player B wins!")
进行测试的更高级(和更多Pythonic)方法是使用元组:
def game(p1, p2):
if p1 == p2:
print("It is a draw!")
elif (p1, p2) in (("rock", "scissors"), ("paper", "rock"), ("scissors", "paper")):
print("Player A wins!")
else:
print("Player B wins!")
答案 1 :(得分:2)
您的代码有几点:
1)我一个人几乎不会拼写"剪刀"正确。如果主要功能只是期待',' p'或者',那么为什么不通过第一个字母,这可能会使游戏更具可玩性game()
的输入?类似的东西:
player_a = input("Player A input one of the following; rock, paper or scissors: ")
payer_a = player_a.strip().lower()[0]
,同样适用于player_b
。
2)我总是将石头剪刀视为一个循环:
R
/ \
S -- P
每个可能性都被顺时针方向的项目所击败,player_2
是否在player_1
的顺时针方向可以通过模运算来确定:
def game(p1,p2):
if p1 == p2:
print("It is a draw!")
else:
i = "rps".index(p1)
if "rps"[(i+1)%3] == p2:
print("Player B wins!")
else:
print("Player A wins!")
这样可以避免案件泛滥。此外,如果你想将游戏扩展到摇滚纸剪刀Spock-lizard"这种东西更容易扩展。大爆炸(表演,而不是宇宙的起源)成名。关于指数的模块化算法是要走的路(虽然"摇滚剪刀-Spock-lizard"需要的不仅仅是简单的方向)。
答案 2 :(得分:1)
或
while True:
if input("Would you like to play again? (Insert Y or N): ") == "Y":
player_a()
player_b()
else:
print('thanks for playing')
break