Home > other >  Variable not showing after resigning
Variable not showing after resigning

Time:10-06

I am making a Tic Tac game, I have marked the places on the board with p1,p2,p3.., so if the user chooses p1, I want it to assign the X or O and show the board with the assigned value. Problem is, the board shows the default, without any change. What am I doing wrong?

#Draws the 'board'
def tabla():
    p1 = ' '
    p2 = ' '
    p3 = ' '
    p4 = ' '
    p5 = ' '
    p6 = ' '
    p7 = ' '
    p8 = ' '
    p9 = ' '
    row1 = print([f'{p1}',f'{p2}',f'{p3}'])
    row2 = print([f'{p4}',f'{p5}',f'{p6}'])
    row3 = print([f'{p7}',f'{p8}',f'{p9}'])

    #Assigns x or 0    
def assigments_of_x_or_o():
    if assigments_of_x_or_o == 'X' or 'x':
        player1 = 'X'
        player2 = 'O'
        print('Player1 will be X, and player2 will be O!')
    elif assigments_of_x_or_o == 'O' or 'o':
        player1 = 'O'
        player2 = 'X'
        print('Player1 will be O, and player2 will be X!')


#Beginning of the program

print('Welcome to my game!\nPlease select the following!')
x_ili_o = input('Do you want to be X or O? ')
assigments_of_x_or_o()

tabla()
koj_place = input('Player1, what place?(1-9): ')
if koj_place == 1:
    p1 = 'X'
tabla()
        
        

CodePudding user response:

I allowed myself to rewrite your whole code just to show how this part could be programmed in a more... elegant and extendable way. You typically never want to use identical variables like p1, p2, ..., instead you should use arrays. And instead of writing separate code for each p1, p2, ..., you just write a loop.

def print_table(table):
    print('r\c ', end='')
    for col_index in range(len(table[0])):
        print(f'{col_index   1} ', end='')
    print()
    for row_index, row in enumerate(table):
        print(f'{row_index   1} │ ', end='')
        for value in row:
            print(f'{value} ',end='')
        print()

def print_players(players):
    for name, symbol in players.items():
        print(f'{name} will be {symbol}!')
     
def create_table(size):
    return [['·' for col in range(table_size)] for row in range(table_size)]
    
def create_players(user_choice, user_name, opponent_name):
    opponent_of = {'X':'O', 'O':'X'}
    players = { user_name: user_choice, opponent_name: opponent_of[user_choice]}
    return players
    
def input_turn(player_name):
    input_str = input(f'{player_name}, what place? (row column): ')
    return map(lambda coord: int(coord)-1, input_str.split())


# Beginning of the program

print('Welcome to my game!\nPlease select the following!')
x_ili_o = input('Do you want to be X or O? ')
players = create_players(x_ili_o.upper(), 'Player1', 'Player2')
print_players(players)

# you may choose a any size
table_size = 5
table = create_table(table_size)
print_table(table)

current_player = 'Player1'
row, col = input_turn(current_player)
table[row][col] = players[current_player]

print_table(table)

This prints

Welcome to my game!
Please select the following!
Do you want to be X or O? x
Player1 will be X!
Player2 will be O!
r\c 1 2 3 4 5 
1 │ · · · · · 
2 │ · · · · · 
3 │ · · · · · 
4 │ · · · · · 
5 │ · · · · · 
Player1, what place? (row column): 2 3
r\c 1 2 3 4 5 
1 │ · · · · · 
2 │ · · X · · 
3 │ · · · · · 
4 │ · · · · · 
5 │ · · · · · 

CodePudding user response:

In case you will be overwhelmed by Alexey's answer, you can make progress with your approach by simply moving p1, p2, ... variables outside the function, so that they don't get forgotten.

p1 = ' '
p2 = ' '
p3 = ' '
p4 = ' '
p5 = ' '
p6 = ' '
p7 = ' '
p8 = ' '
p9 = ' '

#Draws the 'board'
def tabla():
    row1 = print([f'{p1}',f'{p2}',f'{p3}'])
    row2 = print([f'{p4}',f'{p5}',f'{p6}'])
    row3 = print([f'{p7}',f'{p8}',f'{p9}'])

    #Assigns x or 0    
def assigments_of_x_or_o():
    if assigments_of_x_or_o == 'X' or 'x':
        player1 = 'X'
        player2 = 'O'
        print('Player1 will be X, and player2 will be O!')
    elif assigments_of_x_or_o == 'O' or 'o':
        player1 = 'O'
        player2 = 'X'
        print('Player1 will be O, and player2 will be X!')


#Beginning of the program
print('Welcome to my game!\nPlease select the following!')
x_ili_o = input('Do you want to be X or O? ')
assigments_of_x_or_o()

tabla()
koj_place = input('Player1, what place?(1-9): ')
if koj_place == 1:
    p1 = 'X'
tabla()

CodePudding user response:

Just make sure you get the difference between the following:

def add_numbers():
    a = 2
    b = 3
    c = a   b
    print(c)

add_numbers() # a and b are defined inside the function you can not change them, 
              # hence the function will alway print 5


def add_numbers_2(): 
    c = a   b
    print(c)


add_numbers_2() # this will not work as the variables are not defined

def add_numbers_3(a, b): # the numbers are passed as a and b into the function 
    c = a   b
    print(c)

x = 6 
y = 7

add_numbers_3(a = x, b = y)

The problem is that each function has its own namespace, so the variables inside the function can not be changed from outside. Hope this helps and good luck with your project!

  • Related