简体   繁体   中英

Classes 2d lists in python

I have a 2d list and a class to get users input and compare that object to what is stored.

For testing purposes it prints the coordinate and its equal to A but it ignores the if statement by not adding to the count.

Why is it ignoring the if statement

this is my current code

count =0
print self.object[row][col]
if self.object[row][col] == "A":
    count +=1
print count

I know nothing about the type of self.object[row][col] and type matters, as clearly visible below:

>>> 1 == "1"
False

Thus, by guessing, you can try the following comparisons, converting your variable into appropriate type before comparison:

  • converting to string:

     if str(self.object[row][col]) == "1": 
  • converting to integer:

     if int(self.object[row][col]) == 1: 

Let me know if it helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM