Menu
CoddyTech

Recap - Simple Calculator

Part of the Fundamentals section of Coddy's GDScript journey. Lesson 28 of 92.

challenge icon

Challenge

Easy

Build 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
  • "/": n1 divided by n2, 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.0

and for 7, 2 and "/" it is:

result = 3.5

Try 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

Practice on your own: Online GDScript compiler