Python 如何在if语句后清除变量?
我正在实现一个游戏,允许两个玩家从27根棍子中交替选择。每名球员每回合可以拿1、2或3根棍子;被迫拿最后一根棍子的球员输了 我已经完成了大部分代码,但我必须包括验证。除了拿1-3根棍子外,球员不允许拿最后一根棍子。我尝试使用continue语句,但是当player 2超出限制时,程序返回到player 1的回合 以下是我目前掌握的情况:Python 如何在if语句后清除变量?,python,Python,我正在实现一个游戏,允许两个玩家从27根棍子中交替选择。每名球员每回合可以拿1、2或3根棍子;被迫拿最后一根棍子的球员输了 我已经完成了大部分代码,但我必须包括验证。除了拿1-3根棍子外,球员不允许拿最后一根棍子。我尝试使用continue语句,但是当player 2超出限制时,程序返回到player 1的回合 以下是我目前掌握的情况: count = 27 T = False F = False while count > 1: Playerone = int(input("Pl
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
count=27
T=假
F=假
当计数>1时:
Playerone=int(输入(“玩家1的回合,从1-3开始输入”))
如果Playerone<1或Playerone>3:
打印(“错误”)
持续
计数-=游戏管理员
打印(计数)
如果计数=1:
打印(“P1赢”)
打破
如果计数小于1:
打印(“你拿不起那么多棍子”)
持续
Playertwo=int(输入(“玩家2的回合,从1-3开始输入”))
如果Playertwo<1或Playertwo>3:
打印(“错误”)
持续
计数-=Playertwo
打印(计数)
如果计数=1:
打印(“P2赢”)
打破
如果计数小于1:
打印(“你拿不起那么多棍子”)
持续
最后一个if语句就是问题所在
非常感谢您的帮助。确保用户输入有效的一种方法是使用循环 下面是一个您可能使用的函数的快速示例:
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")
def prompt_integer(msg、minval、maxval、err_invalid、err_oob):
尽管如此:
resp=input(msg)#Python3,在Python2中使用原始输入
尝试:
resp=int(resp)
如果minval您的循环流中有一个基本缺陷:不管两个播放器的输入遇到什么问题,您都可以使用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
valid=False
虽然无效:
Playertwo=int(输入(“玩家2的回合,从1-3开始输入”))
如果Playertwo<1或Playertwo>3:
打印(“错误”)
elif计数-播放两次<1:
打印(“你拿不起那么多棍子”)
其他:
有效=真
将此应用于每个玩家的输入。一旦你离开这个循环,你就有了有效的输入。从那里,你可以减少计数并确定是否有人赢了。这就是我要做的,你可能会对类发疯,但这对你现在来说有点太多了(但需要研究一下)。您还应该研究创建方法,但请查看下面我的代码和我留下的注释
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()
def player_input(player_number:int):#这是一个方法,需要向其传递一个整数
p_input=int(输入(“玩家{}的回合,从1-3开始输入:”.format(玩家编号)))
当p_输入<1或p_输入>3时:#这将导致循环不断地要求玩家输入一个数字,直到该数字介于1或3之间。
打印('请选择一个介于1和3'之间的数字')
p_input=int(input(“Player{}的回合,从1-3开始输入:“.format(Player{u number)))#format将用给定变量的值替换“{}”
return p#u input#此“return”行将返回玩家输入的结果
def find_winner(stick_number:int):#stick_number是必需的变量,“:int”要求该变量为整数
胜利者=假
如果斗杆编号=1:
获胜者=正确
return winner#如果找到或没有赢家,此方法将返回
def next_player(player_number:int):#此方法将交换玩家回合
如果玩家号=1:
玩家号=2
elif player_number==2:
玩家号=1
返回玩家号
def start_game(stick_count:int=27):#此方法将启动游戏,'=27'表示您可以给我任何想要的stick count(这是一个整数),但如果您不提供,我将默认使用'27'
玩家号=1
当斗杆计数>1时:#这将循环通过,直到斗杆计数等于或小于1
sticks_to_remove=玩家输入(玩家编号)#我存储播放结果,以防stick_计数低于1,如果计数不低于1,我将移除棍子
如果斗杆计数-斗杆到移除<1:
打印(“你捡不到那么多棍子”)
持续
其他:
棍棒计数-=棍棒到移除#移除棍棒
如果找到赢家(坚持计数):#寻找赢家
打印('Player{}wins!'.格式(Player_编号))
其他:
玩家号=下一个玩家(玩家号)#如果没有赢家,则转到下一个玩家
如果uuu name_uuuu='uuu main_uuuuu':#这表示如果调用此python脚本,则仅自动执行“start_game()”方法,这在以后开始创建跨越多个文件的更复杂python脚本时非常有用。
开始游戏()
作为旁白——在Python中,一般约定是CamelCase中的名称应该引用类(所有caps名称都应该是模块范围的常量);正则变量应为带下划线的小写字母。请看,Python风格指南。代码基本上可以正常工作。我所需要的只是验证,最后一条if语句就是困扰我的地方。如果您想验证更改,请在更改之前进行验证,而不是进行更改,然后需要回滚。你对对象状态进行变异的地方越多,流控制中的错误可能导致不必要的副作用的地方就越多。