2015-05-11 43 views
1

我打算在今年夏天晚些時候去迪斯尼世界旅行,我一直在設法制定一個計劃來計算旅行的大概費用有趣,並儘量避免生鏽。我的問題是,當我嘗試顯示所有計算的值時,我一直收到標題中的錯誤。我的代碼是:打印字符串時出錯:%d格式:需要一個數字,而不是str

###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),我試圖做陳述的變化,但他們的幫助,不給我。我可以單獨打印每個值,但是當我嘗試在這個大塊文本中打印它們時,我會得到我的錯誤。我很感激任何人都必須提供的見解。

+4

如果將它分解成單獨的行,這會更容易**,然後您可以找出哪一個是問題。 – jonrsharpe

+0

這不是一個文檔字符串。 –

+1

如果您有太多的格式說明符,即使刪除了空格,也無法將所有參數放在單個80行字符中,現在可以開始使用命名格式說明符並傳遞字典。 – abarnert

回答

1

可能該錯誤是由%i格式之一引起的。例如,以下代碼:

'this is %i' % '5' 

這將返回相同的錯誤:TypeError: %d format: a number is required, not str

0

在這段代碼中你可以改進很多東西(閱讀評論!)。

嘗試改變變量的類型相應地要打印的內容:

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

當你的函數的工作,這是相當容易出現許多 爭論混合起來。這是一個改進版本。我使用的文本中的一部分,但剩下的應該直截了當:

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中的所有條目。

相關問題