首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的while循环在6次循环后停止,尽管它应该永远继续下去

我的while循环在6次循环后停止,尽管它应该永远继续下去
EN

Stack Overflow用户
提问于 2021-10-25 11:24:32
回答 2查看 41关注 0票数 0

我正在用python编写一个叫做坦克的游戏,本来应该有50轮,但只有6轮就停止了,有人能告诉我发生了什么吗?它也意味着当用户得分达到10分时停止,因为只有6轮,因此它不可能因此而停止。

下面是我的代码:

代码语言:javascript
复制
#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

有人能告诉我为什么会这样吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-25 11:39:21

我对你的"main“做了一些修改:

代码语言:javascript
复制
#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,然后产生了一个异常。对于下一个版本来说,检查用户的输入将是一个真正的改进。

票数 0
EN

Stack Overflow用户

发布于 2021-10-25 11:29:53

我的猜测是GameInput与字符串"True“的比较。

我将允许您查找它,但我担心两个不同类型的比较将降级为bool类型的比较,并且如果int为非零且字符串为非空,则总是成功。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69707204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档