Menu
CoddyTech

Adding The Twist

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

challenge icon

Challenge

Easy

Keep everything you wrote in the previous steps, and add a twist to the rules inside fizzbuzz():

A number that is not divisible by 3 or 7 but contains the digit 3 returns "Almost Fizz" instead of the number. For example, 13 and 23 become Almost Fizz, while 30 is still Fizz and 35 is still Buzz.

To check whether a string contains another string, use in: "3" in "13" is true. The number has to be turned into a string first, so the check is "3" in str(number).

For the input 14, the output is:

Welcome to FizzBuzz!
1
2
Fizz
4
5
Fizz
Buzz
8
Fizz
10
11
Fizz
Almost Fizz
Buzz

Try it yourself

extends Node

func _ready():
	print("Welcome to FizzBuzz!")
	var limit := int(OS.read_string_from_stdin())
	for i in range(1, limit + 1):
		print(fizzbuzz(i))

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