def stick_game():
num_sticks = 11
player = 1
playing = True
print("***** WELCOME TO NIM! *****")
print("Each player will take turns removing")
print("1, 2, or 3 sticks from the initial 11.")
print("The player who removes the last stick wins!")
while playing:
print(f"\nPlayer {player}, it's your turn!")
print(f"Here are the sticks remaining: {'|' * num_sticks}")
move = int(input("How many would you like to remove?"))
while move < 1 or move > 3 or move > num_sticks:
if move > num_sticks:
print("Can not take more sticks than remain. Try again:")
else:
print("Invalid move. Try again:")
move = int(input("Invalid move. Try again: "))
num_sticks -= move
if num_sticks == 0:
print(f"Congratulations Player {player}, you win!")
playing = False
player = 2 if player == 1 else 1
stick_game()
your text
我必须输入 4,0,3,3,3,3,2 我不能使用中断或虽然 true,我猜这是不好的做法。这是我在 python 中的第一个术语,所以如果它很明显,就知道我是一个磨砂膏
这是我得到的错误
这个循环就是问题所在:
while move < 1 or move > 3 or move > num_sticks:
if move > num_sticks:
print("Can not take more sticks than remain. Try again:")
else:
print(" Invalid move. Try again:")
您没有在循环中为
move
分配新值,因此它永远保持其错误值。
您需要在循环内放置另一个
input
语句。