在哪里可以在此代码中添加while循环以请求另一个移动。我真的需要帮助,我无法弄明白。我希望它重新提示移动,所以当移动无效时,它会再次请求另一个移动。如果移动有效,它会要求NEXT移动吗?
def valid1(pile1):
move = False
pile1 = 3
if pile1 == 0:
print 'please choose from pile 2 and 3:'
elif pile1 == 3:
move = int(input('please choose the amount you would like:'))
if move > 0 and move <= 2:
pile1 = pile1 - move
else:
print 'you have entered a invalid move'
return pile1
elif pile1 == 2:
move = int(input('please choose the amount you would like:'))
if move > 0 and move <= 2:
pile1 = pile1 - move
else:
print 'you have entered a invalid move'
return pile1
elif pile1 == 1:
move = int(input('please choose the amount'))
if move > 0 and move <= 1:
pile1 = pile1 - move
else:
print 'you have entered a invalid move'
return pile1
else:
print pile2
print valid1(3)
答案 0 :(得分:0)
我发现有点难以理解你的代码,所以我不确定要循环哪一点,但我认为如果你使用以下一般原则你将解决这个问题:
looping = True
while looping = True :
move = int(input('please choose the amount you would like:'))
if move > 0 and move <= 2:
pile1 = pile1 - move
else:
print 'you have entered a invalid move'
return pile1
在上面的代码中,您将继续循环,因此在您输入移动后将继续询问输入提示。如果你想在满足某个条件时跳出循环,那么在你编码条件后,把looping = False。在python中,布尔值的第一个字母必须大写。还有其他方法可以做到这一点,比如使用break语句等,但这是我喜欢使用的方法。正如我所说,我不确定你想要在代码中循环什么,但是如果你把它全部包含在像上面这样的'while'语句中,它应该是固定的。希望这有帮助!