Getting Input
Part of the Fundamentals section of Coddy's Swift journey — lesson 38 of 86.
Challenge
EasyIn the previous lesson, you printed a welcome message. Now, extend your calculator to gather the necessary information from the user.
After the welcome message, read three inputs from the user:
- The total bill amount (a decimal number)
- The tip percentage (an integer)
- The number of people splitting the bill (an integer)
You will receive the following inputs:
- First line: The bill total as a string (e.g.,
"85.50") - Second line: The tip percentage as a string (e.g.,
"20") - Third line: The number of people as a string (e.g.,
"4")
Use readLine() to read each input and safely unwrap them using if let. Convert the bill total to a Double and the other two values to Int.
Print the values in this exact format:
Welcome to the Bill Split Calculator!
Bill: [billTotal]
Tip: [tipPercentage]%
People: [numberOfPeople]For example, if the inputs are 85.5, 20, and 4, the output should be:
Welcome to the Bill Split Calculator!
Bill: 85.5
Tip: 20%
People: 4Try it yourself
print("Welcome to the Bill Split Calculator!")All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input