我正在做一个练习,创建一个石头剪刀布游戏。
我试图通过设置玩家可以玩游戏的次数来超出练习所要求的范围。玩家可以选择在游戏开始时要玩多少手。
问题是,到目前为止,我还没有将While循环与函数结合使用,并且对此存在疑问,我希望有人可以帮助我解决这个问题,并告诉我哪里出了问题。
另外,我觉得我的代码太长了,关于如何最小化它的任何建议?
更新1 我通过从函数前移while循环并将其应用于最后一部分来使其工作,如下所示: 但是,现在我试图在播放器得分的末尾打印一条消息,但是,Python无法识别出该函数内部的player_1_scroe变量变量。您将如何解决这个问题?
player_1 = input("Enter player 1 Name: ").capitalize()
player_2 = input("Enter player 2 Name: ").capitalize()
game_limit = int(input("Enter how many hands you want to play"))
play_count = 0
def play(usr1, usr2):
player_1_points = 0
player_2_points = 0
if usr1 == usr2:
player_1_points += 1
player_2_points += 1
return "Its a Tie!"
elif usr1 == "r":
if usr2 == "p":
player_2_points += 1
return f"{player_2} wins!"
elif usr1 == "r":
if usr2 == "s":
player_1_points += 1
return f"{player_1} wins!"
elif usr1 == "p":
if usr2 == "r":
player_1_points += 1
return f"{player_1} wins!"
elif usr1 == "p":
if usr2 == "s":
player_2_points += 1
return f"{player_2} wins!"
elif usr1 == "s":
if usr2 == "p":
player_1_points += 1
return f"{player_1} wins!"
elif usr1 == "s":
if usr2 == "r":
player_2_points += 1
return f"{player_2} wins!"
print("Enter hand:\n(R)ock\n(P)aper\n(S)cissors")
while True:
if play_count < game_limit:
game_play1 = input(f"{player_1}, enter your hand: ").lower()
game_play2 = input(f"{player_2}, enter your hand: ").lower()
print(play(game_play1, game_play2))
play_count += 1
elif player_1_points > player_2_points:
print(f"{player_1} is the winer with {player_1_points} points!"
print("Game Over")
break
else:
print(f"{player_2} is the winer with {player_2_points} points!"
print("Game Over")
break
答案 0 :(得分:0)
在函数外声明变量player_1_points和player_2_points。
使用关键字“全局”来更改函数内部的变量。
player_1 = input("Enter player 1 Name: ").capitalize()
player_2 = input("Enter player 2 Name: ").capitalize()
game_limit = int(input("Enter how many hands you want to play"))
play_count = 0
player_1_points = 0
player_2_points = 0
def play(usr1, usr2):
global player_1_points
global player_2_points
if usr1 == usr2:
player_1_points += 1
player_2_points += 1
return "Its a Tie!"
elif usr1 == "r":
if usr2 == "p":
player_2_points += 1
return f"{player_2} wins!"
elif usr1 == "r":
if usr2 == "s":
player_1_points += 1
return f"{player_1} wins!"
elif usr1 == "p":
if usr2 == "r":
player_1_points += 1
return f"{player_1} wins!"
elif usr1 == "p":
if usr2 == "s":
player_2_points += 1
return f"{player_2} wins!"
elif usr1 == "s":
if usr2 == "p":
player_1_points += 1
return f"{player_1} wins!"
elif usr1 == "s":
if usr2 == "r":
player_2_points += 1
return f"{player_2} wins!"
print("Enter hand:\n(R)ock\n(P)aper\n(S)cissors")
while True:
if play_count < game_limit:
game_play1 = input(f"{player_1}, enter your hand: ").lower()
game_play2 = input(f"{player_2}, enter your hand: ").lower()
print(play(game_play1, game_play2))
play_count += 1
elif player_1_points > player_2_points:
print(f"{player_1} is the winer with {player_1_points} points!")
print("Game Over")
break
else:
print(f"{player_2} is the winer with {player_2_points} points!")
print("Game Over")
break