退出后变量不显示



我正在做一个猜谜游戏,我在黑板上用p1,p2,p3标记了位置。,所以如果用户选择p1,我希望它分配X或O,并显示带有分配值的棋盘。问题是,电路板显示的是默认值,没有任何更改。我做错了什么?

#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()


如果您将被Alexey的答案所淹没,您可以通过简单地移动p1,p2,…函数之外的变量,这样它们就不会被遗忘。

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()

我允许自己重写你的整个代码只是为了展示如何这部分可以编程在一个更…优雅和可扩展的方式。你通常不希望使用相同的变量,如p1, p2, ...,相反,你应该使用数组。而不是为每个p1, p2, ...编写单独的代码,您只需编写一个循环。

def print_table(table):
print('rc ', 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)

这个打印

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!
rc 1 2 3 4 5 
1 │ · · · · · 
2 │ · · · · · 
3 │ · · · · · 
4 │ · · · · · 
5 │ · · · · · 
Player1, what place? (row column): 2 3
rc 1 2 3 4 5 
1 │ · · · · · 
2 │ · · X · · 
3 │ · · · · · 
4 │ · · · · · 
5 │ · · · · · 

请确保您了解以下内容之间的区别:

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)

问题是每个函数都有自己的命名空间,所以函数内部的变量不能从外部修改。希望这有助于并祝你的项目好运!

最新更新