0
percentage = int(input("\nWhat percentage of your bill would you like to calculate? "))
percentage = float(round(percentage/100 * bill, 2))
新的Python,不知道上面的代碼兩行是否可縮短至僅包括「百分比=」一次?
由於「百分比」變量是由用戶輸入定義的,我假設在這樣做之前,我無法將「百分比」定義爲方程式,因此兩個單獨的代碼行?
如果它有助於瞭解最終目標,那麼這裏是代碼作爲一個整體 - 這是一個簡單的小費計算器:
# Tip calculator
# calculates a %15 and %20 tip for any given meal
# code by c07vus
print("\nHello, welcome to tip calculator!")
bill = float(input("\nIn £'s what was the bill total? "))
fifteen = float(round(15/100 * bill, 2))
twenty = float(round(20/100 * bill, 2))
print("\nA %15 tip of your bill would be:", fifteen, "pounds")
print("\nA %20 tip of your bill would be:", twenty, "pounds")
print("\nYou can also calculate any percentage you like!")
percentage = int(input("\nWhat percentage of your bill would you like to calculate? "))
percentage = float(round(percentage/100 * bill, 2))
print("\nThank you, your tip would be:", percentage, "pounds")
input("\n\nPress the enter key to exit")
感謝所有幫助和建議!
哇!謝謝! – c07vus