Recap - Safe Division
Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 70 of 78.
Challenge
EasyThe program reads two lines of input: a dividend and a divisor. Both should be numbers, with or without decimals.
- If either line is not a valid number, print
Error: Invalid number - Otherwise, call the function
safe_divide(a, b), which you write above_ready(). It takes two floats and returns a dictionary:{"ok": false, "error": "Cannot divide by zero"}whenbis0{"ok": true, "value": a / b}otherwise
- If the result is ok, print
Division result:and the value with two decimals. Otherwise, printError:and the error message.
For the inputs 10 and 4, the output is:
Division result: 2.50For the inputs 100 and 0, the output is:
Error: Cannot divide by zeroTry it yourself
extends Node
# Write the safe_divide function here
func _ready():
var first := OS.read_string_from_stdin()
var second := OS.read_string_from_stdin()
# Check both inputs, call safe_divide and print the result
pass
All lessons in Logic & Flow
1Strings In Depth
String MethodsSearching in StringsReplace and TrimSplit and JoinRecap - Username Check4Dictionaries Part 2
Nested DictionariesArrays of DictionariesMerging DictionariesCopying DictionariesRecap - Frequency Counter2Advanced Decision Making
Match With VariablesMatch GuardsArray PatternsDictionary PatternsMembership ChecksRecap - Command Parser5Contact Book Application
Display MenuAdd Contact8Student Records Manager
Project OverviewAdd Student3Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysJagged ArraysCommon 2D Array PatternsRecap - Game Board6Sets with Dictionaries
What is a Set?Dictionary as a SetAdding and RemovingUnion and IntersectionSet DifferenceRecap - Remove Duplicates9Advanced Data Aggregation
Summing ValuesFinding Minimum and MaximumCustom SortingRecap - Dictionary Sorter12Basic Error Handling
What is Error Handling?Validating InputGuard ClausesReturning Error CodesRecap - Safe DivisionPractice on your own: Online GDScript compiler