19.Extra Credit


I tried to make a short stuff on battle field game but my loop is not running...Although no error but the answer is getting redundant . There is no pop-up asking my next turn row and column.

Code maybe not up to mark as I am a newbie in python!


raw_input is what prompt the user, so if you want to prompt the user multiply times, you will need to place raw_input inside the loop


I was just typing the same. Just found out.
Thanks !


board[guess_row][guess_col] = "X"
to get the "X" when I use above command it pops error.
Board is 2-dimensional then why error? Do I need to def row and col first?


this depends on your full code, well, you stored the result of the users guesses in ship_row and ship_col variable so guess_row and guess_col would be undefined, but you might have modified this, please copy paste your full code to the forum


for i in range(5):
    print " ".join(i)
from random import randint
rand_row= randint(0,len(board)-1)
for turn in range(4): 
    ship_row=int(raw_input("Enter the row:"))
    ship_col=int(raw_input("Enter the col:"))
    if ship_row==rand_row and ship_col==rand_col:
        print "Congrats !"
    elif (ship_row <0 or ship_row>4) and (ship_col<0 or ship_col>4): 
        if turn==3:
            print "Game Over"
            print "That was way off target. please take another chance"
        print "sorry You are wrong about my location"
    print turn
    if turn==4:
        print "Enough! Your chances are now over"


board[guess_row][guess_col] = "X" is not in the code, and like i already covered guess_row and guess_col are unnamed variable given you store the result of prompting the user in ship_row and ship_col (which are a bit misleading names for this variables)


shouldn't it be board[rand_row][rand_col]="X"
Also, there is a error if I use the variables as you said:-

where I am getting wrong apart from the setting it as guess_row and guess_col ?


this is one of the things i don't understand about your code, on line 8 and 9 you create rand_row and rand_col variable, but this are the ships coordinates so not a logic variable name

on line 14 and 15 you create ship_row and ship_col variable, but this contain the users guesses, so again not logic variables

the reason you get an error is because your rows are strings, while they should be lists


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