Battleship! 12. Bad Aim


#1



Battleship! 12. Bad Aim


There is no error, just an infinite loop!:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 3
Guess Col: 3
pick a number between 0 and 4!!
Guess Row:


guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
if guess_row != range(5) or guess_col != range(5): 
    #if guess_row or guess_col != range(5):
    print "pick a number between 0 and 4!!"
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
elif guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board [guess_row] [guess_col] = "X"
    print_board(board)


#2

here:

if guess_row != range(5) or guess_col != range(5):

why use !=?

a integer will not equal a list, so this condition is always true


#3

Well, the answer to 'why?' is because I don't know what I'm doing! I assumed that was obvious...:slight_smile:

I just changed != to "not in", (as seen in some stackoverflow post) like this:

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
if guess_row or guess_col not in range(5):
    print "pick a number between 0 and 4!!"
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
elif guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board [guess_row] [guess_col] = "X"
    print_board(board)

The condition is still always true.
Do I need to phrase it as a boolean then?

(PS, not sure why the formatting is screwed..)


#4

Also, the hint on this exercise is broken, it gets cut off halfway through


#5

you need to comparison at both sides of the or operator


#6

Thanks! Hard to keep track of which combinations of iterations have been tried..!


#7

do you understand it?


#8

Yeah I understand it (albeit a very basic understanding), just a case of learning the specific syntax and semantics


#9

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