Upgrade the Game by showing the randint result on the board


#1

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.>
https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/2?curriculum_id=4f89dab3d788890003000096#

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

```python

from random import randint
print randint(0,5)

board=[]
for i in range(0,5):
board.append([“O”]*5)
print board

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

def random_r(row):
rand_row=randint(0,len(row)-1)
return rand_row
def random_c(col):
rand_col=randint(0,len(col[0])-1)
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"
else:
board[guess_row][guess_col]="X"
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>

#2

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.


#3

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