打印字符串%d格式时出错:需要数字,而不是

2024-05-01 18:49:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我计划今年夏天晚些时候去迪斯尼乐园旅行,我一直在做一个程序来计算旅行的大概费用,并尽量避免自己变得太生疏。我的问题是,当我试图显示我所有的计算值时,我一直收到标题中的错误。我的代码是:

###Function to display costs
def Display(days, nights, building_type, person, room_cost,
            room_cost_person, DisneyPark, Hopper, IslandPark,
            IslandPTP, Island_parking, gas_cost, gas_cost_person,
            park_person, Total_cost_person, mpg, gas, downpay):
print('''Cost of trip for a %i day/%i night stay in a %%s%%:
Number of people going:                          %i

Total room cost ($)                              %4.2f
Room cost/person ($)                             %4.2f

Price of Disney World tickets ($)                %4.2f
Price of hopper ticket-Disney ($)                %4.2f
Price of Universal ticket ($)                    %4.2f
      Park-to-Park                               %%s%%
Cost to park at Universal/person ($)             %4.2f

Total cost of gas ($)                            %4.2f
Cost of gas/person ($)*                          %4.2f
Cost to park/person ($)                          %4.2f

Cost of groceries/person ($)^                    %4.2f
Cost to eat out/person ($)^#                     %4.2f
Souvenirs ($)^                                   %4.2f

Total cost of trip/person ($)                    %4.2f

*Factoring in round trip distance (1490 miles), mpg of %i, and average gas cost $%4.2f
#Covers eating out at night, eating in parks (butterbeer, etc), and eating while driving
^Note that these are estimates
%Note that the Villa housing requires a $%4.2f downpayment (refundable) that was not
        included in cost calculations

----------------------------------------------------------------------------------------'''
%(day, night, Building, person, room_cost, room_cost_person, DisneyPark,
  Hopper, IslandPark, IslandPTP, Island_parking, gas_cost, gas_cost_person,
  park_person, Groceries, Eat, Souvenirs, Total_cost_person, mpg, gas,
  downpay))

我已经看了这个问题的建议:Python MySQLdb issues (TypeError: %d format: a number is required, not str)并且我试图做出声明中的更改,但是它们对我没有帮助。我可以单独打印每个值很好,但当我试图将它们全部打印在这个大的文本块中时,我会得到我的错误。我很感激任何人提供的见解。


Tags: oftoinparkpricepersontotalroom
3条回答

在这段代码中有很多东西可以改进(请阅读注释!)。

尝试根据要打印的内容相应更改变量类型:

'Print string as %s, integers as %i ou %d, and float as %4.2f' % \
( '5', int('5'), int('5'), float('5') )

该错误可能是由%i格式之一引起的。例如,以下代码:

'this is %i' % '5'

这将返回相同的错误:TypeError: %d format: a number is required, not str

当你的函数工作时,它很容易把许多 论据。这是一个改进的版本。我只使用了正文的一部分,但其余部分应该是直截了当的:

TEMPLATE = """
Cost of trip for a {days} day/{nights} night stay in a %{building_type}%:
Number of people going:                          {person}

Total room cost ($)                              {room_cost:4.2f}
Room cost/person ($)                             {room_cost_person:4.2f}
"""


data = {'days': 3, 'nights': 2, 'building_type': 'hotel', 
        'person': 4, 'room_cost': 80, 'room_cost_person': 20} 

def display_costs(data, template=TEMPLATE):
    """Show the costs in nicely readable format.
    """
    print(template.format(**data))


display_costs(data)    

这张照片:

Cost of trip for a 3 day/2 night stay in a %hotel%:
Number of people going:                          4

Total room cost ($)                              80.00
Room cost/person ($)                             20.00

我在函数外部的模板中定义了文本。你甚至可以定义一个额外的文件来读取这个文件。使用字典data可以帮助组织数据。我对字符串使用了较新的format()方法。这允许您对占位符使用{}语法。你不需要定义字符串和整数的格式,只要你不想让它们以特殊的方式显示在它们的位置等。浮点数的格式可以使用此语法{room_cost:4.2f}指定。这将给您两个小数,总宽度为4,就像旧的%格式一样。最后,我在format()方法中使用了**。这相当于编写template.format(days=3, nights=2, ...),只是不必重复字典中的所有条目data

相关问题 更多 >