擅长:python、mysql、java
<p>给你。你已经有了一个很好的代码。<br/>
看看这些区别:</p>
<p>-您正在重写<code>groceries</code><br/>
-您没有从字典中读取值<br/>
-在python<;3x中,应该使用原始输入而不是输入。如果您在py3k输入正常<br/>
-在py2.x中打印也不需要括号,而在py3k中则需要括号<br/>
-也许唯一奇怪的是,在这里的作业是使用%打印。这意味着%s将被最后一个%符号后的字符串替换<br/>
-要小心,因为食品的成本实际上是字符串,所以不能用它们进行数学运算。首先你应该把它们转换成浮点数。在</p>
<pre><code>def main():
key = ''
infile = open('shoppinglist.txt', 'r')
count = infile.readline()
groceries = {}
print('This program keeps a running total of your shopping list.')
print('Use \'EXIT\' to exit.')
grocery = raw_input('Enter an item: ')
for line in infile:
line = line.strip() #take out newlines codes.
if key == '':
key = line
else:
groceries[key] = line #maybe use here float(line) instead
key = ''
print 'Your current total is %s $' % groceries[grocery]
main()
</code></pre>
<p>对于多个输入使用(未测试,py3k代码,请记住之前将成本转换为浮动):</p>
^{pr2}$