我打算在今年夏天晚些時候去迪斯尼世界旅行,我一直在設法制定一個計劃來計算旅行的大概費用有趣,並儘量避免生鏽。我的問題是,當我嘗試顯示所有計算的值時,我一直收到標題中的錯誤。我的代碼是:打印字符串時出錯:%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),我試圖做陳述的變化,但他們的幫助,不給我。我可以單獨打印每個值,但是當我嘗試在這個大塊文本中打印它們時,我會得到我的錯誤。我很感激任何人都必須提供的見解。
如果將它分解成單獨的行,這會更容易**,然後您可以找出哪一個是問題。 – jonrsharpe
這不是一個文檔字符串。 –
如果您有太多的格式說明符,即使刪除了空格,也無法將所有參數放在單個80行字符中,現在可以開始使用命名格式說明符並傳遞字典。 – abarnert