Why doesnt this code work?


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/2/4?curriculum_id=4f89dab3d788890003000096


ValueError: invalid literal for int() with base 10: 's'


Shouldn't this code print "type numbers only" when I wont input numbers? But when I type any other thing it shows the above error.


 guess_row = (raw_input("Guess Row:"))
 guess_col = (raw_input("Guess Col:"))
    
    if guess_row != int(guess_row) or guess_col != int(guess_col):
        print "Type numbers only."


#2

No, because int(guess_row) does not work if guess_row is a string that's not a valid integer literal.

The comparison is never made if int(guess_row) throws an error.

print int("200") # works
print int("abc") # causes an error

Also guess_row and int(guess_row) will be unequal for all valid inputs because one is an integer and one is a string.

print(2 == "2") # prints False


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.