Upgrade the Game by showing the randint result on the board


I was trying to improve my game code by showing both:
raw_input: "X"
randint result: "R"
But failed, because every time the code recall different randint number between the one compared with the if statement and the one appeared in the else statement.

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

I print the outcome of the random_r or random_c function, which will be compared later with the raw_input guess of the player. Also, I print the outcome of these 2 functions again in the else statement at the end of code to show the player where is the battleship place

<What do you expect to happen instead?>


from random import randint
print randint(0,5)

for i in range(0,5):
print board

def print_board(x):
for i in x:
print " ".join(i)
print print_board(board)

def random_r(row):
return rand_row
def random_c(col):
return rand_col
print “Random row is %s” %random_r(board) ############# This is the 1st randint guess
print “Random col is %s” %random_c(board) #### I tried to give it a variable name & insert it below but failed also

guess_row=int(raw_input("Enter your row guess: "))
guess_col=int(raw_input("Enter your col guess: "))

if random_r(board) == guess_row and random_c(board) == guess_col:
print "Great"
board[random_r(board)][random_c(board)]=“R” ######### This is the 2nd randint guess (different than the 1st)
print print_board(board)
print “Keep on”

<do not remove the three backticks above>


random_r and random_c generate a new random number every time the functions are called

call the functions once, store the returned result by the function in a variable, so we have one set of random coordinate for the entire game.


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