Need Code issue help

def create_board(r):
    print(r, "Create")
    board = list()
    for i in range(r):
        board.append([0 for i in range(7)])
    return board

def check_move(board, turn, col, pop):
    checkmovestatus = False
    print (r, " Check ")
    return checkmovestatus

def check_victory(board, turn):
    print (r,  "Vict" )
    vict_gamewinner=0
    return vict_gamewinner

def apply_move(board, turn, col, pop):
    print (r,  "Apply" )
    return

def player_turn(board, turn):
    checkmovestatus = pop = False
    col=1
    print (r,  "Player" )
    checkmovestatus = check_move(board, turn, col, pop)
    return col, pop

def menu_one():
    r = int(input("Please enter the desired number (5, 6, or 7): "))
    return r
    
def menu_two():
    board = create_board(r)
    gamewinner = 0
    turn=1
    col, pop = player_turn(board, turn)
    apply_move(board, turn, col, pop)    
    gamewinner = check_victory(board, turn)


r  = menu_one()
menu_two()

**** code combining 2 menu *********

def create_board(r):
    print(r, "Create")
    board = list()
    for i in range(r):
        board.append([0 for i in range(7)])
    return board

def check_move(board, turn, col, pop):
    checkmovestatus = False
    print (r, " Check ")
    return checkmovestatus

def check_victory(board, turn):
    print (r,  "Vict" )
    vict_gamewinner=0
    return vict_gamewinner

def apply_move(board, turn, col, pop):
    print (r,  "Apply" )
    return

def player_turn(board, turn):
    checkmovestatus = pop = False
    col=1
    print (r,  "Player" )
    checkmovestatus = check_move(board, turn, col, pop)
    return col, pop

def menu_one():
    r = int(input("Please enter the desired number (5, 6, or 7): "))
    board = create_board(r)
    gamewinner = 0
    turn=1
    col, pop = player_turn(board, turn)
    apply_move(board, turn, col, pop)    
    gamewinner = check_victory(board, turn)


menu_one()

Code - 2 ( I am trying to combine 2 functions into 1 . Variable r from Function 1 ( in code 1) is not accessible from others functions when i am try to combine both functions into 1 - not sure why . How to combine this in single menu , ( Declaring Global - is not the option, also don’t want to pass this variable to various functions )

Could you please format your code in code block as you can see down below?

Paste your code here

Also, please state your traceback for the error as well. And, ask accordingly because right now, I can’t understand what you are trying to ask.

Edit: This should be a good starting point.

Looking at your code, I’m confused as to why each of your functions needs the value that r is assigned to. It appears that the only function using the value is create_board. Could you simply do away with the menu_one function, and get the user’s input in the create_board function? If not, and you do need the value in each of the functions then you’ll either have to pass it around as an argument, or make it globally accessible. That can be done without using the global keyword, but is it necessary? I’d consider implementing a class if that’s something you’re familiar with.

Hi Tod
this code is just for simulating the issue. Actual problem/ Code - uses the variable 'r ’ in all these functions.

  1. one solution i tried - which is working is if i add menu_one.r ( function name.variable name ) - then i can access the variable r in other functions.
  2. I am new to python. I tried to understand concept of classes, but couldn’t figure out - how to implement in this situation. If you can help with simple code / example for this situation.

There are a few ways to make the value available to all functions. If the solution you’ve found works for you, that’s perfectly fine. It’s your program. I’ve included an example of a simple class below. I’d suggest studying up on classes if you choose to go this route. Good luck!

Simple Class example
class Game:
    def __init__(self):
        self.r = None # intial value

    def get_r_from_user(self):
        user_val = None
        while not user_val:
            try:
                user_val = int(input("Blah blah blah? "))
            except ValueError:
                print("Please enter a number between blah and blah.")
                user_val = None
        self.r = user_val
    
    def do_stuff(self):
        print(f'Blah blah {self.r} blah.')
        # More stuff

    def do_other_stuff(self):
        print(f'Blah blah {self.r} blah.')
        # More other stuff

my_game = Game()
my_game.get_r_from_user()
my_game.do_stuff()
my_game.do_other_stuff()
1 Like

Thanks Tod

1 Like