【问题标题】:Cant get a while loop to work the way that I'm intending无法让 while 循环按照我的意图工作
【发布时间】:2020-03-15 16:49:26
【问题描述】:

我正在做一个练习来创建一个石头剪刀布游戏。

我试图通过设置玩家可以玩游戏的次数来超出练习的要求。玩家可以在游戏开始时选择他们想要玩的手数。

问题是,到目前为止,我从未将 While 循环与函数结合使用,而且我遇到了问题,我希望有人能帮助我解决这个问题并告诉我哪里错了。

另外,我觉得我的代码太长了,关于如何最小化它的任何建议?

更新 1 我通过将 while 循环从函数之前移动并将其应用于最后一部分来使其工作,如下所示: 但是现在我试图在最后打印一条带有玩家分数的消息,但是,Python 无法识别 player_1_scroe 变量 beachs 在函数内部。你将如何解决这个问题?

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

【问题讨论】:

  • 您应该在 if 语句中保留 play_count 增量部分,以保持 while 循环正常工作。
  • 感谢 Anus,我更新了我的帖子。我能够修复代码,但是现在我遇到了分数计数的新问题。现在我试图在最后打印一条带有玩家分数的消息,但是,Python 无法识别 player_1_scroe 变量,因为它位于函数内部。你会如何解决这个问题?

标签: python-3.x function while-loop


【解决方案1】:

在函数外部声明你的变量 player_1_points 和 player_2_points。

使用关键字'global'来改变函数内部的变量。

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

【讨论】:

  • 用python尽情享受
猜你喜欢
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多