东东枪 发表于 2018-8-20 21:10:05

为什么我实现出来跟教材不一样啊?新人求助

##嵌套创建多个外星人
alien_0 ={'color':'green','points':5,'speed':'slow'}
alien_1 ={'color':'yellow','points':10,'speed':'medium'}
alien_2 ={'color':'red','points':15,'speed':'quick'}

##aliens =
##for alien in aliens:
##    print(alien)

#创建一个用户存储外星人的空列表
aliens = []

#创建30个绿色外星人
for num in range(0,30):
    aliens.append(alien_2)

for alien in aliens[:5]:
    print(alien)
print('总共创建了'+str(len(aliens))+'个外星人')

#随着游戏的进行,可能需要批量修改外星人的信息。
#修改前三个外星人的颜色、得分、速度
for alien in aliens:   #for循环,遍历列表中的字典列表名[起始索引,结束索引]
    if alien['color'] =='red':
      alien['color'] = 'yellow' #通过字典名['键']='进行赋值,'颜色赋值为 yellow
      alien['points'] = 10   #分数赋值为10
      alien['speed'] = 'medium'#速度赋值为 中等
      print('aasqas')


#显示前5个外星人
for alien in aliens[:5]:
    print(alien)
print('总共创建了'+str(len(aliens))+'个外星人')

print(aliens)

东东枪 发表于 2018-8-20 21:16:48

我实现出来是这个样子的,不仅仅前三个变成黄色,全部都变成了黄色


Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) on win32
Type "copyright", "credits" or "license()" for more information.
>>>
===== RESTART: C:/Users/WangDong/Desktop/复习/python/class1 入门学习/6.4嵌套.py =====
{'color': 'red', 'points': 15, 'speed': 'quick'}
{'color': 'red', 'points': 15, 'speed': 'quick'}
{'color': 'red', 'points': 15, 'speed': 'quick'}
{'color': 'red', 'points': 15, 'speed': 'quick'}
{'color': 'red', 'points': 15, 'speed': 'quick'}
总共创建了30个外星人
aasqas
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
总共创建了30个外星人
[{'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}, {'color': 'yellow', 'points': 10, 'speed': 'medium'}]
>>>

PythonWorld 发表于 2018-8-21 09:56:17

仔细查看一下你的代码,可以拿源码对比一下,看看你的代码是否都写正确了。

东东枪 发表于 2018-8-21 10:33:52

PythonWorld 发表于 2018-8-21 09:56
仔细查看一下你的代码,可以拿源码对比一下,看看你的代码是否都写正确了。

谢谢,我这个贴可能没有说清楚,我重新发了一个贴,比较清晰的描述了问题和期望效果和实际实现效果
可以帮忙看看吗,谢谢?

PythonWorld 发表于 2018-8-21 15:18:04

楼主只需要把变量定义在函数里即可,楼主把成员变量修改了,造成后面的数据全部都修改,一般没有楼主这种操作,源码如下:
# 创建一个用户存储外星人的空列表
aliens = []

# 创建30个绿色外星人
for num in range(0, 30):
    #定义一个局部变量,楼主这里用的是成员变量
    new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
    aliens.append(new_alien)

# 随着游戏的进行,可能需要批量修改外星人的信息。
# 修改前三个外星人的颜色、得分、速度
for alien in aliens:# for循环,遍历列表中的字典列表名[起始索引,结束索引]
    if alien['color'] == 'green':
      alien['color'] = 'yellow'# 通过字典名['键']='进行赋值,'颜色赋值为 yellow
      alien['points'] = 10# 分数赋值为10
      alien['speed'] = 'medium'# 速度赋值为 中等

# 显示前5个外星人
for alien in aliens[:5]:
    print(alien)

print(aliens)
页: [1]
查看完整版本: 为什么我实现出来跟教材不一样啊?新人求助