我的 while 循环在 6 次循环后停止,尽管它本来是要永远持续下去的
My while loop stops after 6 loops despite the fact it is meant to go on forever
我正在 python 中编写一个名为坦克的游戏,本应有 50 个回合,但它在只有 6 个回合后停止,谁能告诉我发生了什么。它也意味着当用户的分数达到 10 时停止,因为只有 6 轮,因此无法停止。
这是我的代码:
#Tanks problem
import random
Grid = [["" for X in range(8)] for Y in range(8)]
PlacePicked=[]
#subroutine to place tanks
def TankPlace():
global Grid
for I in range(0,10,1):
while True:
X=random.randint(0,7)
Y=random.randint(0,7)
if Grid[X][Y]=="T":
pass
else:
Grid[X][Y]="T"
break
#subroutine to output places already picked
def OutputPlacePicked():
global PlacePicked
Places=""
if len(PlacePicked)==0:
pass
else:
for I in range(0,len(PlacePicked),1):
Places=Places+PlacePicked[I]+", "
print("\nSo far you have picked the coordinates of {}".format(Places[:-2]))
#subroutine to check if inputted coordinate is a tank or not
def GameInput(X,Y):
X1=X-1
Y1=Y-1
global Grid
global PlacePicked
while True:
if "({} ,{})".format(X,Y) in PlacePicked:
print("\nYou have already chosen ({}, {})".format(X, Y))
X=int(input("\nWhat is your X coordinate?\n"))
Y=int(input("\nWhat is your Y coordinate?\n"))
elif Grid[X1][Y1]=="T":
print("\nTank Hit!")
PlacePicked.append("({} ,{})".format(X, Y))
tank="True"
return tank
else:
print("\nTank Missed!")
PlacePicked.append("({} ,{})".format(X, Y))
tank="False"
return tank
#maincode
print("Welcome to Tanks!\n")
print("Instructions:\n-Tanks is a game for 1 player.\n-At the start of the game, the computer places 10 tanks on an 8x8 board but does not\nreveal their locations to the player.\n-Each tank occupies one square on the board.\n-The player enters a grid reference, e.g. (1,5) on each turn.\n-The player destroys the tank if the square is occupied by a tank.\n-The player wins if they destroy all the tanks within 50 turns.")
go=0
score=0
while True:
TankPlace()
OutputPlacePicked()
GameInput(int(input("\nWhat is your X coordinate?\n")), int(input("\nWhat is your Y coordinate?\n")))
if GameInput=="True":
score=score+1
elif GameInput=="False":
pass
if score==10:
print("You Win!")
break
else:
pass
if go==50:
print("You Lose")
break
else:
pass
go=go+1
谁能告诉我为什么会这样?
我的猜测是将已转换为 int 的 GameInput 与字符串“True”进行比较
我会让你查一下,但我担心两种不同类型的比较会降级为 bool 类型的比较,并且如果 int 非零且字符串非空,则总是会成功..
我对你的“主要”做了一点修改:
#maincode
print("Welcome to Tanks!\n")
print("""Instructions:
-Tanks is a game for 1 player.
-At the start of the game, the computer places 10 tanks on an 8x8 board but does not
reveal their locations to the player.
-Each tank occupies one square on the board.
-The player enters a grid reference, e.g. (1,5) on each turn.
-The player destroys the tank if the square is occupied by a tank.
-The player wins if they destroy all the tanks within 50 turns.""")
go=0
score=0
TankPlace()
while True:
OutputPlacePicked()
GameInput(int(input("\nWhat is your X coordinate?\n")), int(input("\nWhat is your Y coordinate?\n")))
if GameInput=="True":
score=score+1
elif GameInput=="False":
pass
if score==10:
print("You Win!")
break
else:
pass
if go==50:
print("You Lose")
break
else:
pass
go=go+1
请注意,我已将调用移至循环之前的 TankPlace()
;我认为每次移动都重新放置坦克不会帮助玩家获胜。
我还删除了 global tank
因为它没有被使用。
反正后来我能下6步以上了。
尽管如此,游戏还是提前结束了,因为我猜到了 9,结果出现异常。下一个版本检查用户输入将是一个真正的改进。
我正在 python 中编写一个名为坦克的游戏,本应有 50 个回合,但它在只有 6 个回合后停止,谁能告诉我发生了什么。它也意味着当用户的分数达到 10 时停止,因为只有 6 轮,因此无法停止。
这是我的代码:
#Tanks problem
import random
Grid = [["" for X in range(8)] for Y in range(8)]
PlacePicked=[]
#subroutine to place tanks
def TankPlace():
global Grid
for I in range(0,10,1):
while True:
X=random.randint(0,7)
Y=random.randint(0,7)
if Grid[X][Y]=="T":
pass
else:
Grid[X][Y]="T"
break
#subroutine to output places already picked
def OutputPlacePicked():
global PlacePicked
Places=""
if len(PlacePicked)==0:
pass
else:
for I in range(0,len(PlacePicked),1):
Places=Places+PlacePicked[I]+", "
print("\nSo far you have picked the coordinates of {}".format(Places[:-2]))
#subroutine to check if inputted coordinate is a tank or not
def GameInput(X,Y):
X1=X-1
Y1=Y-1
global Grid
global PlacePicked
while True:
if "({} ,{})".format(X,Y) in PlacePicked:
print("\nYou have already chosen ({}, {})".format(X, Y))
X=int(input("\nWhat is your X coordinate?\n"))
Y=int(input("\nWhat is your Y coordinate?\n"))
elif Grid[X1][Y1]=="T":
print("\nTank Hit!")
PlacePicked.append("({} ,{})".format(X, Y))
tank="True"
return tank
else:
print("\nTank Missed!")
PlacePicked.append("({} ,{})".format(X, Y))
tank="False"
return tank
#maincode
print("Welcome to Tanks!\n")
print("Instructions:\n-Tanks is a game for 1 player.\n-At the start of the game, the computer places 10 tanks on an 8x8 board but does not\nreveal their locations to the player.\n-Each tank occupies one square on the board.\n-The player enters a grid reference, e.g. (1,5) on each turn.\n-The player destroys the tank if the square is occupied by a tank.\n-The player wins if they destroy all the tanks within 50 turns.")
go=0
score=0
while True:
TankPlace()
OutputPlacePicked()
GameInput(int(input("\nWhat is your X coordinate?\n")), int(input("\nWhat is your Y coordinate?\n")))
if GameInput=="True":
score=score+1
elif GameInput=="False":
pass
if score==10:
print("You Win!")
break
else:
pass
if go==50:
print("You Lose")
break
else:
pass
go=go+1
谁能告诉我为什么会这样?
我的猜测是将已转换为 int 的 GameInput 与字符串“True”进行比较
我会让你查一下,但我担心两种不同类型的比较会降级为 bool 类型的比较,并且如果 int 非零且字符串非空,则总是会成功..
我对你的“主要”做了一点修改:
#maincode
print("Welcome to Tanks!\n")
print("""Instructions:
-Tanks is a game for 1 player.
-At the start of the game, the computer places 10 tanks on an 8x8 board but does not
reveal their locations to the player.
-Each tank occupies one square on the board.
-The player enters a grid reference, e.g. (1,5) on each turn.
-The player destroys the tank if the square is occupied by a tank.
-The player wins if they destroy all the tanks within 50 turns.""")
go=0
score=0
TankPlace()
while True:
OutputPlacePicked()
GameInput(int(input("\nWhat is your X coordinate?\n")), int(input("\nWhat is your Y coordinate?\n")))
if GameInput=="True":
score=score+1
elif GameInput=="False":
pass
if score==10:
print("You Win!")
break
else:
pass
if go==50:
print("You Lose")
break
else:
pass
go=go+1
请注意,我已将调用移至循环之前的 TankPlace()
;我认为每次移动都重新放置坦克不会帮助玩家获胜。
我还删除了 global tank
因为它没有被使用。
反正后来我能下6步以上了。 尽管如此,游戏还是提前结束了,因为我猜到了 9,结果出现异常。下一个版本检查用户输入将是一个真正的改进。