无法在 python3 上分配运算符
Cannot assign operator on python3
我正在学习 python 并尝试使我的代码发挥作用。
## set total sticks number
stick_left = 20
## Given hints, this for statement shows sticks
for _ in range(5): print('| '*stick_left)
## this condition lasts until stick = 0
while stick_left>0:
## first player
first_player=input('Player1: please pick sticks up to 3 ')
if first_player>3 or first_player<0:
print('Please pick between 1-3 stick(s)')
else:
stick_left-=first_player
if stick_left<=0:
print ('Player1 lost')
break
else:
print('There is %d stick(s) left' %stick_left) ## print how many sticks left
for _ in range(5): print('| ' * stick_left)
## second player
second_player = input('Player2: Please pick sticks up to 3 ')
if second_player > 3 or second_player < 0:
print('Please pick between 1-3 stick(s)')
else:
stick_left -= second_player
if stick_left <= 0:
print ('Player2 lost')
break
else:
print('There is %d stick(s) left' % stick_left)
for _ in range(5): print('| ' * stick_left)
这是我的代码包含函数
player =0
def player():
for i in 2:
player%d %i= input('Player %d: Please pick sticks up to 3' %i)
if player >3 or player <0:
print('Please pick between 1-3 stick(s)')
在第 4 行,我想让它打印 player1 player2,但我只是注意到我不能那样分配运算符。
我试过 player_%d %i
但没有成功。有人可以给我建议吗?
谢谢!
您不能像这样即时定义变量名。使用字典。
players = {}
def player():
for i in range(2):
players[i] = input("Player %d: Please pick sticks up to 3" % (i,))
# etc
exec() 函数将为您工作。请参考以下代码。
player =0
def player():
for i in range(1,3):
exec("player%d = input('Player%d = Please pick sticks up to 3 ')" %(i,i))
player()
以下是o/p.
Player1 = Please pick sticks up to 3 1
Player2 = Please pick sticks up to 3 2
我正在学习 python 并尝试使我的代码发挥作用。
## set total sticks number
stick_left = 20
## Given hints, this for statement shows sticks
for _ in range(5): print('| '*stick_left)
## this condition lasts until stick = 0
while stick_left>0:
## first player
first_player=input('Player1: please pick sticks up to 3 ')
if first_player>3 or first_player<0:
print('Please pick between 1-3 stick(s)')
else:
stick_left-=first_player
if stick_left<=0:
print ('Player1 lost')
break
else:
print('There is %d stick(s) left' %stick_left) ## print how many sticks left
for _ in range(5): print('| ' * stick_left)
## second player
second_player = input('Player2: Please pick sticks up to 3 ')
if second_player > 3 or second_player < 0:
print('Please pick between 1-3 stick(s)')
else:
stick_left -= second_player
if stick_left <= 0:
print ('Player2 lost')
break
else:
print('There is %d stick(s) left' % stick_left)
for _ in range(5): print('| ' * stick_left)
这是我的代码包含函数
player =0
def player():
for i in 2:
player%d %i= input('Player %d: Please pick sticks up to 3' %i)
if player >3 or player <0:
print('Please pick between 1-3 stick(s)')
在第 4 行,我想让它打印 player1 player2,但我只是注意到我不能那样分配运算符。
我试过 player_%d %i
但没有成功。有人可以给我建议吗?
谢谢!
您不能像这样即时定义变量名。使用字典。
players = {}
def player():
for i in range(2):
players[i] = input("Player %d: Please pick sticks up to 3" % (i,))
# etc
exec() 函数将为您工作。请参考以下代码。
player =0
def player():
for i in range(1,3):
exec("player%d = input('Player%d = Please pick sticks up to 3 ')" %(i,i))
player()
以下是o/p.
Player1 = Please pick sticks up to 3 1
Player2 = Please pick sticks up to 3 2