Recap - Simple Calculator
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 28 of 92.
Challenge
EasyBuild a small calculator. The variables n1, n2 and op are read for you on the first three lines (do not change them), and op holds one of "+", "-", "*" or "/".
Set result based on op:
"+":n1 + n2"-":n1 - n2"*":n1 * n2"/":n1divided byn2, keeping the decimal part
Write all four cases: the tests try each operator. result is a float, so it always prints with a decimal point. For example, for 12, 34 and "+" the output is:
result = 46.0and for 7, 2 and "/" it is:
result = 3.5Try it yourself
extends Node
func _ready():
var n1 := int(OS.read_string_from_stdin()) # Don't change this line
var n2 := int(OS.read_string_from_stdin()) # Don't change this line
var op := OS.read_string_from_stdin() # Don't change this line
var result: float = 0.0
# Write your code below
# Don't change the line below
print("result = ", result)
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 OperatorPractice on your own: Online GDScript compiler