Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading