2017-04-26 86 views
0

這是我的一些Python代碼:如何刪除python中的額外空間?

my_fancy_variable = input('Choose a color to paint the wall:\n') 

if my_fancy_variable == 'red': 
    print(('Cost of purchasing red paint:\n$'),math.ceil(paint_needed) * 35) 
elif my_fancy_variable == 'blue': 
    print(('Cost of purchasing blue paint:\n$'),math.ceil(paint_needed) * 25) 
elif my_fancy_variable == 'green': 
    print(('Cost of purchasing green paint:\n$'),math.ceil(paint_needed) * 23) 

我只是想擺脫「$」之間的空間和「105

有更多的代碼,但基本上我會得到一個結果: Cost of purchasing red paint: $ 105

感謝

+0

@CaptainTrunky這個問題涉及到Python 2 –

回答

1

打印功能有一個默認的說法,sep,這是考慮到打印本功能每個參數之間的分隔符離子。

默認情況下,它被設置爲一個空格。您可以輕鬆地這樣修改它,(在你的情況沒有什麼):

print('Cost of paint: $', math.ceil(paint_needed), sep='') 
# Cost of paint: $150 

如果你想每個參數以換行符分開,你可以這樣做:

print('Cost of paint: $', math.ceil(paint_needed), sep='\n') 
# Cost of paint: $ 
# 150 

sep可您需要(或想要)的任何字符串值。

+0

我相信這個問題想與 –

+0

我回答第二個例子中,整條生產線後的金額和換行的美元符號 –

0

我會用格式化字符串的可讀性:

f"Cost of purchasing blue paint: ${math.ceil(paint_needed) * 25}" 

另一個這裏關鍵是你有多少IFS要補充的嗎?靛藍/橙色等

colours = { 
    'red': "$35", 
    'blue': "$25", 
    'green': "$23" 
} 

cost = colours.get(my_fancy_variable, "Unknown cost") 

print(f"Cost of purchasing {my_fancy_variable} is {cost}") 
相關問題