Python 如何为纸牌游戏修复for循环?

Python 如何为纸牌游戏修复for循环?,python,for-loop,Python,For Loop,这个问题看起来很简单,但我一直坚持下去。。。我想创建多轮基于for循环的纸牌游戏,直到计算机或用户获胜。我的for循环不起作用,在说出剩下多少张卡后立即停止 我已经尝试过处理缩进和for循环的样式 def card_game(): print("New Game start") cards = int(21) comp_pick = 0 user_choice = input("Do you want to go first? y/n: ")

这个问题看起来很简单,但我一直坚持下去。。。我想创建多轮基于for循环的纸牌游戏,直到计算机或用户获胜。我的for循环不起作用,在说出剩下多少张卡后立即停止

我已经尝试过处理缩进和for循环的样式

def card_game():

    print("New Game start")

    cards = int(21)

    comp_pick = 0


    user_choice = input("Do you want to go first? y/n: ")


    if user_choice == "y":

        for n in range(cards):

            while n < 21 and n > 0:

                return print("There are",cards - comp_pick,"cards left")

            user_pick = int(input("How many cards do you pick? (1-3): "))
            comp_pick = int(4 - user_pick)

            if user_pick > int(3):
                return print("You cannot pick",user_pick,"cards")

            elif user_pick < int(1):
                return print("You cannot pick",user_pick,"cards")

            else:
                cards = cards - user_pick
                if comp_pick == 1:
                    return print("There are",cards,"left \n\tI pick",comp_pick,"card")
                else:
                    return print("There are",cards,"left \n\tI pick",comp_pick,"cards")

            n = cards - comp_pick


    if user_choice == "n":
        #assuming the computer will always pick 1 card first
        #taking 1 card will allow the number of cards to remain divisible by 4

        return print("There are",cards,"cards left \n\tI pick 1 card \nThere are 20 cards left")

        for n in range(1, 20):

            while n < 20 and n > 0:

                user_pick = int(input("How many cards do you pick? (1-3): "))

            if user_pick > int(3):
                return print("You cannot pick",user_pick,"cards")

            if user_pick < int(1):
                return print("You cannot pick",user_pick,"cards")

            else:
                cards = cards - user_pick
                comp_pick = 4 - user_pick
                if comp_pick == 1:
                    return print("There are",cards,"left \n\tI pick",comp_pick,"card")
                else:
                    return print("There are",cards,"left \n\tI pick",comp_pick,"cards")

            n = cards - comp_pick

这取决于用户是否决定先使用。我得到了前几行,但是我的
for
循环在下一场比赛中不会继续。

就像萨尔说的那样,
return
语句将退出函数。此外,for语句中会覆盖行
n=cards-comp_pick
。请参见此示例:

范围(10)内的n的
:
打印(n)
n+=2
有人会认为它打印:
0,2,4,…18
,但实际上它打印:
0,1,2。。。9
。这是因为
range
函数返回一个延迟计算的列表。因此,上述代码块可以写成:

[0,1,2,3,4,5,6,7,8,9]中的n的

打印(n)
n+=2

这里很明显,在
n+=2
行之后,
n
被分配给列表中的下一个元素。

就像萨尔所说的那样,
return
语句将退出函数。此外,for语句中会覆盖行
n=cards-comp_pick
。请参见此示例:

范围(10)内的n的
:
打印(n)
n+=2
有人会认为它打印:
0,2,4,…18
,但实际上它打印:
0,1,2。。。9
。这是因为
range
函数返回一个延迟计算的列表。因此,上述代码块可以写成:

[0,1,2,3,4,5,6,7,8,9]中的n的

打印(n)
n+=2

这里很明显,在
n+=2
行之后,
n
被分配到列表中的下一个元素。

主要问题肯定是
返回print
语句,并且正如您所意识到的,当
循环时,实际上不需要
。由于
y/n
问题后面的两个块几乎相同,您可以创建一个函数,使其合并代码,并使其更具可读性。我在这里和那里调整了其他一些小事情,但仍然缺少获胜的逻辑。无论如何,希望这对现在和将来都有帮助

    def card_loop(start_count):
        cards = start_count  # how many cards we start with, as it depends

        while cards > 0:  # it is always decremented, so just check for zero threshold
            print("There are", cards, "cards left")
            user_pick = int(input("How many cards do you pick? (1-3): "))
            if 1 <= user_pick <= 3:
                # valid user pick, proceed with calculations
                cards -= user_pick  # cards left from user pick
                comp_pick = 4 - user_pick  # computer pick logic
                print("There are", cards, "left \n\tI pick", comp_pick, "card")
                cards -= comp_pick  # cards left from computer pick

            else:
                # invalid user pick, do nothing, will ask user to pick again
                print("You cannot pick", user_pick, "cards")


    def card_game():
        print("New Game start")
        user_choice = input("Do you want to go first? y/n: ")

        if user_choice == "y":
            card_loop(21)

        elif user_choice == "n":
            # assuming the computer will always pick 1 card first
            # taking 1 card will allow the number of cards to remain divisible by 4
            card_loop(20)


    card_game()
def卡循环(开始计数):
卡片=开始计数#我们开始时有多少张卡片,视情况而定
当卡片>0时:#它总是递减,所以只需检查零阈值
打印(“有”,卡片,“剩下的卡片”)
用户_pick=int(输入(“您选择了多少张卡?(1-3):”)

如果1主要问题肯定是
返回打印
语句,并且正如您所意识到的,没有真正需要
while
循环。由于
y/n
问题后面的两个块几乎相同,您可以创建一个函数,使其合并代码,并使其更具可读性。我在这里和那里调整了其他一些小事情,但仍然缺少获胜的逻辑。无论如何,希望这对现在和将来都有帮助

    def card_loop(start_count):
        cards = start_count  # how many cards we start with, as it depends

        while cards > 0:  # it is always decremented, so just check for zero threshold
            print("There are", cards, "cards left")
            user_pick = int(input("How many cards do you pick? (1-3): "))
            if 1 <= user_pick <= 3:
                # valid user pick, proceed with calculations
                cards -= user_pick  # cards left from user pick
                comp_pick = 4 - user_pick  # computer pick logic
                print("There are", cards, "left \n\tI pick", comp_pick, "card")
                cards -= comp_pick  # cards left from computer pick

            else:
                # invalid user pick, do nothing, will ask user to pick again
                print("You cannot pick", user_pick, "cards")


    def card_game():
        print("New Game start")
        user_choice = input("Do you want to go first? y/n: ")

        if user_choice == "y":
            card_loop(21)

        elif user_choice == "n":
            # assuming the computer will always pick 1 card first
            # taking 1 card will allow the number of cards to remain divisible by 4
            card_loop(20)


    card_game()
def卡循环(开始计数):
卡片=开始计数#我们开始时有多少张卡片,视情况而定
当卡片>0时:#它总是递减,所以只需检查零阈值
打印(“有”,卡片,“剩下的卡片”)
用户_pick=int(输入(“您选择了多少张卡?(1-3):”)

如果1您的循环逻辑是复杂的。此外,您还试图一次编写多个游戏功能,而不测试其中任何一个,这使得您当前的调试更加困难。让我们来处理游戏循环。分解层次结构,可能是这样的:

# Play one game.
# Start with 20 cards
# Continue until there are no cards left

deck = 20
while deck > 0:
    # Each player gets a turn: human first
    human_take = int(input("How many cards do you want?"))
    deck -= human_take
    print("You took", human_take, "cards; there are", deck, "remaining")

    computer_take = 4 - human_take
    deck -= computer_take
    print("You took", computer_take, "cards; there are", deck, "remaining")
这就是你可能想要的游戏循环的要点。我省略了输入验证(在这个网站上有很多关于这个主题的答案),以及其他一些事情


你能从那里继续吗?

你的循环逻辑很复杂。此外,您还试图一次编写多个游戏功能,而不测试其中任何一个,这使得您当前的调试更加困难。让我们来处理游戏循环。分解层次结构,可能是这样的:

# Play one game.
# Start with 20 cards
# Continue until there are no cards left

deck = 20
while deck > 0:
    # Each player gets a turn: human first
    human_take = int(input("How many cards do you want?"))
    deck -= human_take
    print("You took", human_take, "cards; there are", deck, "remaining")

    computer_take = 4 - human_take
    deck -= computer_take
    print("You took", computer_take, "cards; there are", deck, "remaining")
这就是你可能想要的游戏循环的要点。我省略了输入验证(在这个网站上有很多关于这个主题的答案),以及其他一些事情


你能从那里继续吗?

你有太多的
return
语句。如果您只需要打印,您只需要
打印
,而不需要
返回打印
。这就是为什么在打印字符串时代码会立即退出。谢谢!我去掉了那些,去掉了不必要的while循环。我添加了一个答案,其中包含了更多的代码,如果你想查看的话,可能会有帮助。你有太多的
return
语句。如果您只需要打印,您只需要
打印
,而不需要
返回打印
。这就是为什么在打印字符串时代码会立即退出。谢谢!我去掉了那些,去掉了不必要的while循环。我添加了一个答案,其中包含更多代码,如果你想查看的话,可能会有帮助。是的,谢谢。我现在正在编写最后一段代码,以确保当剩余的卡值达到1或0时,正确的输出将显示在我的Shell中,游戏将结束。是的,谢谢。我现在正在编写最后一段代码,以确保当剩余的卡值达到1或0时,正确的输出将显示在我的Shell中,游戏将结束。这非常有帮助。谢谢这帮了大忙。谢谢