Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AttributeError(艰苦学习Python)_Python_Python 2.7 - Fatal编程技术网

AttributeError(艰苦学习Python)

AttributeError(艰苦学习Python),python,python-2.7,Python,Python 2.7,我只是想找出这段代码返回错误的原因。我有两个属性错误,不知道为什么 错误1 Traceback (most recent call last): File "ex39a.py", line 47, in <module> for console, abbrev in consoles.items(): AttributeError: 'str' object has no attribute 'items' 在控制台字典和游戏字典中循环时,不应使用相同的迭代器名称。这样

我只是想找出这段代码返回错误的原因。我有两个属性错误,不知道为什么

错误1

Traceback (most recent call last):
  File "ex39a.py", line 47, in <module>
    for console, abbrev in consoles.items():
AttributeError: 'str' object has no attribute 'items'

控制台
字典和
游戏
字典中循环时,不应使用相同的迭代器名称。这样,
控制台
以及
游戏
字典就被转换成其他字符串。试试这个:

consoles = {
    'Nintendo Entertainment System': 'NES',
    'SEGA Saturn': 'Saturn',
    'Playstation 3': 'PS3',
    'Xbox 360': '360',
    'Atari 2600': '2600'
}

games = {
    'NES': 'Castlevania',
    'Saturn': 'SEGA Rally Championship',
    'PS3': 'Assassins Creed 2'
}
games['360'] = 'Halo 3'
games['2600'] = 'Space Invaders'

print '-' * 10
print "The 360 has: ", games['360']
print "The 2600 has: ", games ['2600']

print '-' * 10
print "The Atari 2600s abbreviation is: ", consoles['Atari 2600']
print "The Nintendo Entertainment Systems abbreviation is: ", consoles['Nintendo Entertainment System']

print '-' * 10
print "The Atari 2600 has: ", games[consoles['Atari 2600']]
print "The Playstation 3 has: ", games[consoles['Playstation 3']]

print '-' * 10
# CHANGE HERE
for console, abbrev in consoles.items():
    print "%s is abbreviated %s" % (console, abbrev)

print '-' * 10
# CHANGE HERE
for abbrev, game in games.items():
    print "%s has the game %s" % (abbrev, game)
print '-' * 10
for console, abbrev in consoles.items():
    print "%s console is abbreviated %s and has the game %s" % (
        console, abbrev, games[abbrev])
print '-' * 10
#safely get an abbreviation for a console that might not be there
console = consoles.get('SEGA Dreamcast', None)

if not console:
    print "Sorry, that console is too shit to list."


game = games.get('Resident Evil: Code Veronica', 'Does Not Exist')
print "That game is for the: %s" % game 

您在控制台.items()中为控制台重写了
控制台
的值,abbrev在consoles.items()
中。与第36行中的游戏相同,
为abbrev,games
谢谢。我将“控制台”的每一个用法都改为“硬件”,将“游戏”改为“软件”,以消除混淆。
consoles = {
    'Nintendo Entertainment System': 'NES',
    'SEGA Saturn': 'Saturn',
    'Playstation 3': 'PS3',
    'Xbox 360': '360',
    'Atari 2600': '2600'
}

games = {
    'NES': 'Castlevania',
    'Saturn': 'SEGA Rally Championship',
    'PS3': 'Assassins Creed 2'
}
games['360'] = 'Halo 3'
games['2600'] = 'Space Invaders'

print '-' * 10
print "The 360 has: ", games['360']
print "The 2600 has: ", games ['2600']

print '-' * 10
print "The Atari 2600s abbreviation is: ", consoles['Atari 2600']
print "The Nintendo Entertainment Systems abbreviation is: ", consoles['Nintendo Entertainment System']

print '-' * 10
print "The Atari 2600 has: ", games[consoles['Atari 2600']]
print "The Playstation 3 has: ", games[consoles['Playstation 3']]

print '-' * 10
for consoles, abbrev in consoles.items():
    print "%s is abbreviated %s" % (consoles, abbrev)

print '-' * 10
for abbrev, games in games.items():
    print "%s has the game %s" % (abbrev, games)
#error  
print '-' * 10
for console, abbrev in consoles.items():
    print "%s console is abbreviated %s and has the game %s" % (
        console, abbrev, games[abbrev])
#error          
print '-' * 10
#safely get an abbreviation for a console that might not be there
console = consoles.get('SEGA Dreamcast', None)

if not console:
    print "Sorry, that console is too shit to list."


game = games.get('Resident Evil: Code Veronica', 'Does Not Exist')
print "That game is for the: %s" % game 
consoles = {
    'Nintendo Entertainment System': 'NES',
    'SEGA Saturn': 'Saturn',
    'Playstation 3': 'PS3',
    'Xbox 360': '360',
    'Atari 2600': '2600'
}

games = {
    'NES': 'Castlevania',
    'Saturn': 'SEGA Rally Championship',
    'PS3': 'Assassins Creed 2'
}
games['360'] = 'Halo 3'
games['2600'] = 'Space Invaders'

print '-' * 10
print "The 360 has: ", games['360']
print "The 2600 has: ", games ['2600']

print '-' * 10
print "The Atari 2600s abbreviation is: ", consoles['Atari 2600']
print "The Nintendo Entertainment Systems abbreviation is: ", consoles['Nintendo Entertainment System']

print '-' * 10
print "The Atari 2600 has: ", games[consoles['Atari 2600']]
print "The Playstation 3 has: ", games[consoles['Playstation 3']]

print '-' * 10
# CHANGE HERE
for console, abbrev in consoles.items():
    print "%s is abbreviated %s" % (console, abbrev)

print '-' * 10
# CHANGE HERE
for abbrev, game in games.items():
    print "%s has the game %s" % (abbrev, game)
print '-' * 10
for console, abbrev in consoles.items():
    print "%s console is abbreviated %s and has the game %s" % (
        console, abbrev, games[abbrev])
print '-' * 10
#safely get an abbreviation for a console that might not be there
console = consoles.get('SEGA Dreamcast', None)

if not console:
    print "Sorry, that console is too shit to list."


game = games.get('Resident Evil: Code Veronica', 'Does Not Exist')
print "That game is for the: %s" % game