19.Extra Credit


#1

Hi
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!
thanks


#2

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


#3

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


#4

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?
@stetim94


#5

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


#6

board=[]
for i in range(5):
    i=("O")*5
    board.append(i)
    print " ".join(i)
from random import randint
rand_row= randint(0,len(board)-1)
rand_col=randint(0,len(board)-1)
turn=0
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 !"
        break
    elif (ship_row <0 or ship_row>4) and (ship_col<0 or ship_col>4): 
        if turn==3:
            print "Game Over"
        else:
            print "That was way off target. please take another chance"
    else:
        print "sorry You are wrong about my location"
    turn=turn+1
    print turn
    if turn==4:
        print "Enough! Your chances are now over"

#7

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)


#8

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 ?


#9

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


#10

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