Menu
CoddyTech

Looping The Numbers

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

challenge icon

Challenge

Easy

Keep 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
Buzz

Try 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

Practice on your own: Online GDScript compiler