为什么字符串格式的全局变量在更改 .format 中的变量后保持不变?
Why does string-formatted global variable remain the same after changing the variables in the .format?
python 的新手,尝试编写一个井字游戏的代码,结果遇到这种情况,我似乎无法打印出我的 table 版本(多行字符串) 将新的 O 分配给来自输入的位置,即使在将 O 分配给该位置的变量之后也是如此。
下面的代码不完整,暂时只关注上面的问题。
mydict=dict.fromkeys(['a1','a2','a3','b1','b2','b3','c1','c2','c3'],' ')
table = ' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
' | | '.format(mydict['a1'],mydict['a2'],mydict['a3'],mydict['b1'],mydict['b2'],mydict['b3'],mydict['c1'],mydict['c2'],mydict['c3'])
def assign(i):
if q%2==0:
mydict[str(i)]='O'
print(mydict)
else:
mydict[str(i)]='X'
q=2
while q%2==0:
x=input('Player 1, please choose your position:')
assign(x)
print(table)
当我运行:
TIC TAC TOE
| |
| |
_______|_______|_______
| |
| |
_______|_______|_______
| |
| |
| |
Player 1, please choose your position:a1
{'a1': 'O', 'a2': ' ', 'a3': ' ', 'b1': ' ', 'b2': ' ', 'b3': ' ', 'c1': ' ', 'c2': ' ', 'c3': ' '}
| |
| |
_______|_______|_______
| |
| |
_______|_______|_______
| |
| |
| |
Player 1, please choose your position:
如你所见,O并没有出现在a1位置
修改 mydict
还不够,您还需要进行一些其他更改:
mydict=dict.fromkeys(['a1','a2','a3','b1','b2','b3','c1','c2','c3'],' ')
def table():
table = ' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
' | | '.format(mydict['a1'],mydict['a2'],mydict['a3'],mydict['b1'],mydict['b2'],mydict['b3'],mydict['c1'],mydict['c2'],mydict['c3'])
return table
def assign(i, q):
if q%2 == 0:
mydict[str(i)]='O'
print(mydict)
else:
mydict[str(i)]='X'
print(table())
q=0
while ' ' in mydict.values():
x=input('Player %s, please choose your position:' % ((q % 2) + 1))
q += 1
assign(x, q)
格式化只会创建字符串的新副本,不会应用于任何进一步的更改。您应该创建一个函数来格式化 table 并使用基本模板。
table = (' | | \n'
' {} | {} | {} \n'
'_______|_______|_______\n'
' | | \n'
' {} | {} | {} \n'
'_______|_______|_______\n'
' | | \n'
' {} | {} | {} \n'
' | | ')
def format_table():
return table.format(mydict['a1'], mydict['a2'],
mydict['a3'], mydict['b1'],
mydict['b2'], mydict['b3'],
mydict['c1'], mydict['c2'],
mydict['c3'])
现在,当您打印 table 时,只需执行以下操作:
print(format_table())
代码不起作用的原因是每次打印变量时都不会调用格式 table。为了保留格式并使用新值更新字符串,您需要每次都调用它。我的建议如下。
mydict=dict.fromkeys(['a1','a2','a3','b1','b2','b3','c1','c2','c3'],' ')
tablestring = ' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
' | | '
def assign(i):
if q%2==0:
mydict[str(i)]='O'
print(mydict)
print(tablestring.format(mydict['a1'],mydict['a2'],mydict['a3'],mydict['b1'],mydict['b2'],mydict['b3'],mydict['c1'],mydict['c2'],mydict['c3']))
else:
mydict[str(i)]='X'
python 的新手,尝试编写一个井字游戏的代码,结果遇到这种情况,我似乎无法打印出我的 table 版本(多行字符串) 将新的 O 分配给来自输入的位置,即使在将 O 分配给该位置的变量之后也是如此。
下面的代码不完整,暂时只关注上面的问题。
mydict=dict.fromkeys(['a1','a2','a3','b1','b2','b3','c1','c2','c3'],' ')
table = ' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
' | | '.format(mydict['a1'],mydict['a2'],mydict['a3'],mydict['b1'],mydict['b2'],mydict['b3'],mydict['c1'],mydict['c2'],mydict['c3'])
def assign(i):
if q%2==0:
mydict[str(i)]='O'
print(mydict)
else:
mydict[str(i)]='X'
q=2
while q%2==0:
x=input('Player 1, please choose your position:')
assign(x)
print(table)
当我运行:
TIC TAC TOE
| |
| |
_______|_______|_______
| |
| |
_______|_______|_______
| |
| |
| |
Player 1, please choose your position:a1
{'a1': 'O', 'a2': ' ', 'a3': ' ', 'b1': ' ', 'b2': ' ', 'b3': ' ', 'c1': ' ', 'c2': ' ', 'c3': ' '}
| |
| |
_______|_______|_______
| |
| |
_______|_______|_______
| |
| |
| |
Player 1, please choose your position:
如你所见,O并没有出现在a1位置
修改 mydict
还不够,您还需要进行一些其他更改:
mydict=dict.fromkeys(['a1','a2','a3','b1','b2','b3','c1','c2','c3'],' ')
def table():
table = ' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
' | | '.format(mydict['a1'],mydict['a2'],mydict['a3'],mydict['b1'],mydict['b2'],mydict['b3'],mydict['c1'],mydict['c2'],mydict['c3'])
return table
def assign(i, q):
if q%2 == 0:
mydict[str(i)]='O'
print(mydict)
else:
mydict[str(i)]='X'
print(table())
q=0
while ' ' in mydict.values():
x=input('Player %s, please choose your position:' % ((q % 2) + 1))
q += 1
assign(x, q)
格式化只会创建字符串的新副本,不会应用于任何进一步的更改。您应该创建一个函数来格式化 table 并使用基本模板。
table = (' | | \n'
' {} | {} | {} \n'
'_______|_______|_______\n'
' | | \n'
' {} | {} | {} \n'
'_______|_______|_______\n'
' | | \n'
' {} | {} | {} \n'
' | | ')
def format_table():
return table.format(mydict['a1'], mydict['a2'],
mydict['a3'], mydict['b1'],
mydict['b2'], mydict['b3'],
mydict['c1'], mydict['c2'],
mydict['c3'])
现在,当您打印 table 时,只需执行以下操作:
print(format_table())
代码不起作用的原因是每次打印变量时都不会调用格式 table。为了保留格式并使用新值更新字符串,您需要每次都调用它。我的建议如下。
mydict=dict.fromkeys(['a1','a2','a3','b1','b2','b3','c1','c2','c3'],' ')
tablestring = ' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
'_______|_______|_______\n' \
' | | \n' \
' {} | {} | {} \n' \
' | | '
def assign(i):
if q%2==0:
mydict[str(i)]='O'
print(mydict)
print(tablestring.format(mydict['a1'],mydict['a2'],mydict['a3'],mydict['b1'],mydict['b2'],mydict['b3'],mydict['c1'],mydict['c2'],mydict['c3']))
else:
mydict[str(i)]='X'