出力を整形する
CoddyのPythonジャーニー「基礎」セクションの一部。レッスン 34/77。
チャレンジ
初心者このプロジェクトの最後のステップは、出力を整形することです!
たとえば、次の入力の場合:
100 #bill
5 #tip percent
2 #number of people次の形式で出力します:
Bill Split Calculator
Total (including tip): $105.0
Each person pays: $52.5重要な書式設定の注意点:
- 大文字と記号をすべて含め、表示されているものと完全に同じ出力にすること
- 数値を丸めないこと - Python のデフォルトの浮動小数点数の書式を使用すること(.2f などは使わない)
- 数値を float 型に変換すると、Python は小数点以下の桁数をさまざまに表示します(例:105.0、52.5、164.28571428571428)
- 計算した値を直接挿入するには、f-string を使用します:
f"${variable_name}"
自分で試してみよう
print("Bill Split Calculator")
bill_amount = float(input())
tip_percentage = float(input())
num_people = int(input())
tip_amount = (tip_percentage / 100) * bill_amount
total_amount = bill_amount + tip_amount
amount_per_person = total_amount / num_people
print(total_amount)
print(amount_per_person)基礎のすべてのレッスン
自分で練習してみよう: Pythonオンラインコンパイラ