And Seek Question


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/0/8?curriculum_id=4f89dab3d788890003000096



When I originally wrote the solution to the "Hide" problem i set the functions random_row and random_col to return rr and rc respectively which worked fine. As I moved forward to the seek problem my code was reset and i was easily able to complete the step with the reset code. What I don't understand is why this code doesn't work as well? I expected to see this code pass rr and rc to ship_row and ship_col respectively without problem. Clearly it doesn't and I am not understanding why.


from random import randint 
board = []
grid = int(raw_input("What size grid"))
for x in range(0, grid):
    board.append(["O"] * grid)

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

def random_row(board):
    rr = randint(0,len(board)-1)
    return rr
    
def random_col(board):
    rc = randint(0,len(board)-1)
    return rc

random_row(board)
random_col(board)
ship_row = rr
ship_col = rc 
# Add your code below!
guess_row=int(raw_input("Guess Row:"))
guess_column=int(raw_input("Guess Column:"))


#2

Functions have their own variables which aren't shared globally


#3

I thought the return function allowed them to be used globally so that is clearly my issue. Is there a command that would pass the function variable to global status, or is this something I should not have learned yet?


#4

The global scope can be access but your function doesn't really have any business changing it.

If you do 1 + 1, would you expect the result to be placed in a variable? No, the result should be returned so that you can then for example use the result to add more: 1 + 1 + 3

return is not a function, it's a statement.
A function accepts input and produces output, if possible without communicating with the outside by other means than that


#5

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