Skip to content
Advertisement

int unexpectedly turning into string

With the following snippet from a script:

    Tile = SelectTile()
    int(Tile)
    TileY = Tile
    TileX = 0
    #Temporary debug:
    print("type of tile is: ", type(Tile), "type of TileY is: ", type(TileY))

I expected the outcome to be:

type of tile is:  <class 'int'> type of TileY is:  <class 'int'>

but instead I get:

type of tile is:  <class 'str'> type of TileY is:  <class 'str'>

Does anyone know why this is happening and how I can fix it? In case you need it here is SelectTile:

def SelectTile():
    print("Select Tile to put "O" in:")
    print("")
    print(" 1 ॥ 2 ॥ 3 ")
    print("===॥===॥===")
    print(" 4 ॥ 5 ॥ 6 ")
    print("===॥===॥===")
    print(" 7 ॥ 8 ॥ 9 ")
    Tile = input()
    int(Tile)
    #if Tile != 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9:
    #    print("Invalid Tile! Try again")
    #    SelectTile()
    return Tile

Advertisement

Answer

You are not assigning the new int value to the original variable. Here’s a fix:

Tile = SelectTile()
Tile = int(Tile)
TileY = Tile
TileX = 0
#Temporary debug:
print("type of tile is: ", type(Tile), "type of TileY is: ", type(TileY))
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement