我正在函数中返回一个变量,并尝试以全局方式打印它(作为范围测试),但是它不起作用。我需要另一个函数中的变量,但无论做什么,它始终无法识别它。为什么此代码中的最后一个打印无效?
def assignchar():
assigning = True
player2 = ""
while assigning:
player1 = input("Player 1: Do you want to be X or O? ")
if player1.upper() == 'X':
player1 = 'X'
player2 = 'O'
assigning = False
elif player1.upper() == 'O':
player1 = 'O'
player2 = 'X'
assigning = False
else:
print('Please select X or O.')
print(f'Player 1 is {player1}')
print(f'Player 2 is {player2}\n')
return(player1, player2)
assignchar()
print(player1 + "and " + player2)
答案 0 :(得分:0)
假设您需要打印返回的值,我将添加此答案
player1, player2 = assignchar()
print(player1 + "and " + player2)