如何制作一个循环,为我的尼姆再次重复一个循环

问题描述 投票:0回答:1
# importing random module, for random choices
import random

# print_move function to display CPU and players current move
def print_move(current_player, row, stick_quantity):
    current_player = current_player
    if stick_quantity == 1:
        print(f"{current_player} takes {stick_quantity} sticks from {row} row")
    else:
        print((f"{current_player} takes {stick_quantity} sticks from {row} row"))
        

# beginning of nims game, choosing who will choose  
print("Welcome to Nim!")
name = input("Enter name: ")
current_player = random.choice([name, "CPU"])
print('game is starting',(current_player),'will be going first')

# create stick lists
sticks = [1, 3, 5, 7]

# looping the sticks 
def nims():
    for count, stick in enumerate(sticks):
        print(count+1, stick * "|")

# loop that breaks when the number of sticks are less than 0
while sum(sticks) > 0:
    nims()

    if current_player == name:
# exception handling so that the user inputs the correct amount of sticks/rows
        while True:
            try:
                row = int(input("Which row would you like to take sticks from? "))
                stick_quantity = int(input("How many sticks would you like to take? "))
                if 1 <= row <= len(sticks) and 1 <= stick_quantity <= sticks[row-1]:
                    break
                else:
                    print("")
                    print("Invalid input, please enter a valid row, and or a valid amount of sticks")
            except ValueError:
                print("")
                print("Invalid input, please enter a valid row, and or a valid amount of sticks")

        sticks[row - 1] -= stick_quantity
        print_move(current_player,row,stick_quantity)
        if current_player == name:
            current_player = "CPU"
        else:
            current_player = name
    
    else:
        # handling value error in the stick_quantity variable
        try:
            row = random.randint(1, len(sticks))
            stick_quantity = random.randint(1, sticks[row-1])
            sticks[row-1] -= stick_quantity
            print_move(current_player,row,stick_quantity)
            if current_player == "CPU":
                current_player = name
            else:
                current_player = "CPU"
        except ValueError:
            print("")


winner = current_player
print("Winner is", winner)

我已经用 python 创建了 Nims 游戏,一切正常,除了再次重复循环时遇到问题。我想这样做,以便在游戏结束并宣布获胜者后,系统会再次询问您,您想再玩一次吗?如果用户输入“y”,则 Nims 游戏会再次重复,但如果用户按“n”或任何其他相关内容,游戏结束,并将显示用户获胜和失败的分数。

我不确定我是否应该在 nims 游戏开始时有一个 While play_again == 'y': 某种循环,我尝试过这样做,但这似乎不起作用。我也尝试过其他的事情,我不会解释,但到目前为止,它们对我来说还不够好。任何帮助或指导将不胜感激。预先感谢。

python loops while-loop do-while break
1个回答
0
投票

你可以尝试这样的事情:

print("Welcome to Nim!") 
name = input("Enter name: ") 
replay = 'y' 

while replay == 'y':

然后在游戏结束时:

winner = current_player print("Winner is", winner)
replay = str(input("One more round (y/n)?"))
© www.soinside.com 2019 - 2024. All rights reserved.