酒店销售人员在文本文件中输入销售。每行包含以下内容,以分号分隔:客户的名称,销售的服务(如晚餐,会议,住宿等),销售金额以及该事件的日期。编写一个读取此类文件的程序,并显示每个服务类别的总金额。
显示错误我的文字文件包含
Bob;Dinner;10.00;January 1, 2015
Tom;Dinner;14.00;January 2, 2015
Anne;Lodging;125.00;January 3, 2015
Jerry;Lodging;125.00;January 4, 2015
所以这是我的代码到目前为止
def main():
file_name = input("Input file name: ")
amount_by_category = process_file(file_name)
if amount_by_category:
print 'Totals:'
for key in amount_by_category:
print '{0}: $ {1}'.format(key, amount_by_category.get(key) )
def process_file(file_name):
infile = open(file_name, 'r')
# a dictionary mapping category to total amount for that category
amount_by_category = {}
for line in infile:
fields = line.split(';')
if len(fields) != 4:
raise Exception('Expected 4 fields but found %s' % len(fields))
value = float(fields[2])
category = fields[1]
if not category in amount_by_category:
amount_by_category[category] = 0.0
amount_by_category[category] += value
return amount_by_category
main()
我收到语法错误,不知道原因。
追溯:
Traceback (most recent call last):
File "C:\Users\Brandon\Desktop\Assignment 7\girrrr.py",
line 24, in <module> main() File "C:\Users\Brandon\Desktop\Assignment 7\girrrr.py",
line 7, in main print ('{0}: $ {1}').format(key, amount_by_category.get(key) )
AttributeError: 'NoneType' object has no attribute 'format'
答案 0 :(得分:0)
看起来你正在使用一个旧的python,但这个代码在3.6中运行得很好:
def main():
file_name = input("Input file name: ")
amount_by_category = process_file(file_name)
if amount_by_category:
print ("Totals:")
for key in amount_by_category:
print ("{}: $ {}".format(key, amount_by_category.get(key)))
def process_file(file_name):
infile = open(file_name, 'r')
# a dictionary mapping category to total amount for that category
amount_by_category = {}
for line in infile:
fields = line.split(';')
if len(fields) != 4:
raise Exception('Expected 4 fields but found %s' % len(fields))
value = float(fields[2])
category = fields[1]
if not category in amount_by_category:
amount_by_category[category] = 0.0
amount_by_category[category] += value
return amount_by_category
main()
答案 1 :(得分:0)
您共享的回溯如下所示:
Option Explicit
Public Shp As Shape
Sub Bubble2()
' set object reference to new created Shape
Set Shp = ActiveSheet.Shapes.AddShape(msoShapeCloudCallout, 795, 8.25, 107.25, 41.25)
With Shp
.Name = "zooky"
.Adjustments.Item(1) = -0.25029
.TextFrame2.TextRange.Characters.Text = "text.................."
End With
With Shp.TextFrame2.TextRange.Characters(1, 10)
With .ParagraphFormat
.FirstLineIndent = 0
.Alignment = msoAlignLeft
End With
With .Font
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.ObjectThemeColor = msoThemeColorLight1
.Fill.ForeColor.TintAndShade = 0
.Fill.ForeColor.Brightness = 0
.Fill.Transparency = 0
.Fill.Solid
.Size = 11
.Name = "+mn-lt"
End With
End With
Range("P5").Select
' just for testing
'Call FlipFlop2
End Sub
Sub FlipFlop2()
With Shp
.Visible = Not .Visible
End With
End Sub
,而您的代码如下所示:
print ('{0}: $ {1}').format(key, amount_by_category.get(key) )
我尝试运行你的代码,运行正常。尝试重新运行代码,它应该可以工作。