3

I am planning on taking a trip to Disney World late this summer and I have been trying to make a program to calculate an approximate cost of the trip for fun and to try and keep myself from getting too rusty. My problem is that when I try to display all of my calculated values, I keep receiving the error that is in the title. My code is:

###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))

I've looked at the suggestions for this question:Python MySQLdb issues (TypeError: %d format: a number is required, not str) and I tried to make the changes stated but they were not of help to me. I can individually print each value just fine but when I try to print them all in this large block of text I then get my error. I'd appreciate any insight anyone has to offer.

Community
  • 1
  • 1
John
  • 33
  • 1
  • 1
  • 4

3 Answers3

1

Likely the error is caused by one of the %i formattings. For example, the following code:

'this is %i' % '5'

This will return the same error: TypeError: %d format: a number is required, not str.

0

There are a lot of things you can improve in this code (read the comments!).

Try changing the variable type accordingly to what you want to print:

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

While your function works, it is rather prone to mixing up the many arguments. This is an improved version. I used only part of the text but the rest should be straight forward:

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)    

This prints:

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

I defined the text in a template outside the function. You could even define it an extra file and read this file. Using a dictionary data can help to organize your data. I used the newer format() method for strings. This allows you to use the {} syntax for your place holders. You don’t need to define formats for strings and integers as long as you don’t want them displayed in a special way in terms of their position or such. The format for floats can be specified using this syntax {room_cost:4.2f}. This gives you two decimals and a total width of four just as with the old %-formatting. Finally, I used ** in the format() method. This is equivalent to writing template.format(days=3, nights=2, ...) except that don’t have to repeat all the entries from the dictionary data.

Mike Müller
  • 71,943
  • 15
  • 139
  • 140