我正在实施一项游戏,允许两名玩家从一堆27支球队中交替选择。每个球员每回合可以拿1,2或3支球;球员被迫拿走最后一棒。
我已经完成了大部分代码,但我必须包含验证。除了1-3支球队外,球员不允许拿最后一根棍子。我尝试过使用continue语句,但当玩家2超过限制时,程序将返回到玩家1。
这是我到目前为止所拥有的:
count = 27
T = False
F = False
while count > 1:
Playerone = int(input("Player 1's Turn, Enter from 1-3"))
if Playerone < 1 or Playerone > 3:
print("Error")
continue
count -= Playerone
print(count)
if count == 1:
print("P1 wins")
break
if count < 1:
print("You can't pick up those many sticks")
continue
Playertwo = int(input("Player 2's Turn, Enter from 1-3"))
if Playertwo < 1 or Playertwo > 3:
print("Error")
continue
count -= Playertwo
print(count)
if count == 1:
print("P2 wins")
break
if count < 1:
print("You can't pick up those many sticks")
continue
最后一个if语句是问题
非常感谢帮助,
答案 0 :(得分:1)
你的循环流程存在一个基本缺陷:无论播放器输入遇到什么问题,你都可以使用continue
返回循环顶部,这会让你回到播放器1你需要解决这个问题:循环播放给定玩家的输入,直到它在所有方面都有效。这样的事情应该做:
valid = False
while not valid:
Playertwo = int(input("Player 2's Turn, Enter from 1-3"))
if Playertwo < 1 or Playertwo > 3:
print("Error")
elif count - Playertwo < 1:
print("You can't pick up those many sticks")
else:
valid = True
将此应用于每位玩家的输入。一旦你离开这个循环,你就有了有效的输入。从那里,您可以减少计数并确定某人是否赢了。
答案 1 :(得分:0)
确保有效用户输入的一种方法是使用循环。
以下是您可能使用的功能的快速示例:
def prompt_integer(msg, minval, maxval, err_invalid, err_oob):
while True:
resp = input(msg) # Python3, use raw_input in Python2
try:
resp = int(resp)
if minval <= resp <= maxval:
return resp
else:
print(err_oob)
except ValueError:
print(err_invalid)
x = prompt_integer("Enter an integer: ", 1, 3, "Invalid Integer.", "Integer Out of Bounds")
此处,在用户输入1到3(含)之间的有效整数之前,函数不会返回。
如果他们输入&#39; abc&#39;,程序将显示&#34;无效的整数。&#34;并再次问他们。
如果他们输入,比如5,当你指定边界是1和3时,程序将显示&#34;整数超出边界&#34;,然后再次询问他们。
当此功能返回时,您知道您已获得可接受的值。
您可以在代码中使用此功能,并在每次调用maxval参数时根据他们能够拾取的数量来修改。
答案 2 :(得分:0)
这就是我要做的事情,你可能会对Classes
发疯,但现在对你来说有点太多了(但有些事情需要研究)。您还应该研究创建方法,但请查看下面的代码和我留下的评论。
def player_input(player_number: int): # This is a method, that requires an integer to be pass to it
p_input = int(input("Player {}'s Turn, Enter from 1-3: ".format(player_number)))
while p_input < 1 or p_input > 3: # This will cause a loop continuously asking the player to input a number until that number is between 1 or 3.
print('Please choose a number between 1 and 3')
p_input = int(input("Player {}'s Turn, Enter from 1-3: ".format(player_number))) # Format will replace the '{}' with the value of the variable you give it
return p_input # This 'return' line will return the what the results of what the player typed in
def found_winner(stick_number: int): # stick_number is a required variable and ': int' requires that that variable be an integer
winner = False
if stick_number == 1:
winner = True
return winner # This method will return if a winner is found or not
def next_player(player_number: int): # This method will swap the players turn
if player_number == 1:
player_number = 2
elif player_number == 2:
player_number = 1
return player_number
def start_game(stick_count: int = 27): # This method will start the game, the '= 27' says that you can give me any stick count you want(that is an integer), but if you don't provide one I will use '27' by default
player_number = 1
while stick_count > 1: # This will loop through until the stick count is 1 or less
sticks_to_remove = player_input(player_number) # I store the plays result just in case the stick_count goes below 1, and then I remove the sticks if the the count doesn't go below 1
if stick_count - sticks_to_remove < 1:
print('You cant pick up that many sticks')
continue
else:
stick_count -= sticks_to_remove # Remove the sticks
if found_winner(stick_count): # Look for the winner
print('Player {} wins!'.format(player_number))
else:
player_number = next_player(player_number) # If no winner go to the next player
if __name__ == '__main__': # This says only execute the 'start_game()' method automatically if this python script is called, this is useful later when you start creating more complicated Python scripts that span multiple files.
start_game()