Python 3.7.2 Elif 仅在 if 语句中不起作用
Python 3.7.2 Elif not working only if statement
当我打印词典d
时,只有white
变成了Mindful
,其余的颜色不变。
import random
clrs = ['white', 'blue', 'black', 'purple', 'pink', 'yellow']
d = {}
while True:
spawn = int(input('enter how many creatures to spawn: '))
for x in range(spawn):
clr = random.choice(clrs)
print (clr)
if clr=='white':
clr='Mindful'
elif clr=='blue':
clr=='Anima'
elif clr=='black':
clr=='Dimension'
elif clr=='purple':
clr=='Enigma'
elif clr=='pink':
clr=='Light'
elif clr=='yellow':
clr=='Golem'
else:
print ('wtf how have you done this')
d["ball{0}".format(x)]=clr
print (d)
clr=='Anima'
^^
您认为这实际上会改变 clr
吗?我认为不是:-)
也许 =
这里可能更好(其他所有非白人也是如此。
有关详细信息,请参阅以下抄本,==
形成表达式,而 =
赋值:
>>> xyzzy = 'plugh'
>>> xyzzy == 'twisty' ; print(xyzzy)
False
plugh
>>> xyzzy = 'twisty' ; print(xyzzy)
twisty
当我打印词典d
时,只有white
变成了Mindful
,其余的颜色不变。
import random
clrs = ['white', 'blue', 'black', 'purple', 'pink', 'yellow']
d = {}
while True:
spawn = int(input('enter how many creatures to spawn: '))
for x in range(spawn):
clr = random.choice(clrs)
print (clr)
if clr=='white':
clr='Mindful'
elif clr=='blue':
clr=='Anima'
elif clr=='black':
clr=='Dimension'
elif clr=='purple':
clr=='Enigma'
elif clr=='pink':
clr=='Light'
elif clr=='yellow':
clr=='Golem'
else:
print ('wtf how have you done this')
d["ball{0}".format(x)]=clr
print (d)
clr=='Anima'
^^
您认为这实际上会改变 clr
吗?我认为不是:-)
也许 =
这里可能更好(其他所有非白人也是如此。
有关详细信息,请参阅以下抄本,==
形成表达式,而 =
赋值:
>>> xyzzy = 'plugh'
>>> xyzzy == 'twisty' ; print(xyzzy)
False
plugh
>>> xyzzy = 'twisty' ; print(xyzzy)
twisty