Looping The Numbers
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 60 of 92.
Challenge
EasyKeep the welcome message, the input line and the fizzbuzz() function from the previous steps.
Now play the whole game. Remove the line print(fizzbuzz(limit)) and replace it with a for loop over the numbers from 1 to limit, both included. In each iteration, print the value fizzbuzz() returns for that number.
For the input 7, the output is:
Welcome to FizzBuzz!
1
2
Fizz
4
5
Fizz
BuzzTry it yourself
extends Node
func _ready():
print("Welcome to FizzBuzz!")
var limit := int(OS.read_string_from_stdin())
print(fizzbuzz(limit))
func fizzbuzz(number: int) -> String:
var result := ""
if number % 3 == 0:
result += "Fizz"
if number % 7 == 0:
result += "Buzz"
if result == "":
result = str(number)
return 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