我试图在一秒钟内添加第一个字典的最大键,但是当我运行它时,显示sack{}
的语法错误。我似乎无法弄清楚为什么会出现这种错误。任何人都可以解释错误是什么?谢谢。
守则如下:
#!/usr/bin/python3
import sys
def vault(dataFile):
gem = []
weight = []
value = []
valPerCarat = []
for l in dataFile.readlines():
attribute = l.split(",")
gem.append(str(attribute[0]))
weight.append(float(attribute[1]))
value.append(int(attribute[2]))
valPerCarat.append(int(attribute[2])/float(attribute[1]))
booty = theft_preparation(gem, valPerCarat)
return (booty)
#return(gem, weight, value, valPerCarat)
def theft_preparation(gem, valPerCarat):
loot = dict(zip(gem, valPerCarat)
sack = {}
precious = 0
while (precious =< 5.00):
max (loot, key=loot.get)
precious = dict.get(key, default=None)
precious +=precious
sack.update(key)
del(key)
return (sack)
def main():
fileFound = False
while not fileFound:
fileName = input("File name containing jewel data: ")
try:
dataFile = open(fileName, "r")
fileFound = True
except:
print("Could not find that file -- try again")
knapsackCapacity = 5.00
print (vault(dataFile))
main()
答案 0 :(得分:2)
您错过了上一行的右括号:
loot = dict(zip(gem, valPerCarat)
^ HERE