Formatted Output
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 42 of 92.
Challenge
EasyThe last step is to make the output readable. Replace the two lines that print total_amount and amount_per_person with two lines that use a format string and %.2f, so every amount has 2 decimal places.
For example, for the inputs 100, 5 and 2, the program prints:
Bill Split Calculator
Total (including tip): $105.00
Each person pays: $52.50Match the text exactly, including the capital letters, the colon and space, and the $ sign before each amount. Keep the welcome message and the code that reads the inputs and calculates the amounts.
Try it yourself
extends Node
func _ready():
print("Bill Split Calculator")
var bill_amount := float(OS.read_string_from_stdin())
var tip_percentage := float(OS.read_string_from_stdin())
var num_people := int(OS.read_string_from_stdin())
var tip_amount := (tip_percentage / 100) * bill_amount
var total_amount := bill_amount + tip_amount
var amount_per_person := total_amount / num_people
print(total_amount)
print(amount_per_person)
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting InputPractice on your own: Online GDScript compiler