0

Using the fizzbuzz program below on Python 3.10 on Windows 10 (64-bit) gives the output

1
2   
Fizz
4   
Buzz
(...)
14
FizzBuzz

Whereas running it on MicroPython 1.9.4 on a Casio PRIZM fx-CG50 (a graphing calculator) gives the following output

1
2
Fizz
4
Buzz
(...)
14
BuzzFizz

The last line of the above output is highly unexpected based on my understanding of how my fizzbuzz program works and differs between Python on Windows and MicroPython on the calculator. Help diagnosing this irregularity and any advice on who to alert(if any) for a fix(if needed) would be greatly appreciated.

Perhaps the way I have written the program causes the execution order to be ambiguous, or there is some implementation detail I have overlooked.

The exact same program file was run on both systems.

No warnings or errors came up on both runs.

The fizzbuzz program:

def fizzbuzz(num: float, ruleset: dict) -> str:
    output = "" # Declare Output string
    
    rule_string = ruleset # Vanity variable for readability
    # Ruleset is dict in format {float:str, float:string, ...}
    # The floats are the divisors, the strings are the rulestrings
    
    # If divisor is factor of num append corresponding rule_string to output
    for divisor in ruleset:
        output += rule_string[divisor] if (num%divisor)==0 else ""
    
    # If no divisors are factors of num(output is empty string),
    #  append num to output
    output += str(num) if output == "" else ""

    return output

# The classic fizzbuzz ruleset
classic_rules = {
    3:"Fizz",
    5:"Buzz"
}

# Test for numbers 1 to 50 inclusive with classic_fizzbuzz_ruleset
for i in range(1, 16):
    print(fizzbuzz(i, classic_rules))
  • I don't know where to source the files for the specific MicroPython version installed on the calculator as it comes preinstalled with the calculator's Operating System and is accessible via the preinstalled Python app. – AzureArmageddon Sep 03 '22 at 16:34
  • 4
    My initial thought is that MicroPython handles your rules `dict` differently. Python 3.10 will keep the ordering you set when you define it. Perhaps you should use a list of tuples since you don't actually need the look up property of a `dict`. – quamrana Sep 03 '22 at 16:36
  • @quamrana I never thought to put my data in a tuple because I never knew you could put other data types in there as well, but I guess that *is* a good solution since I can index lists by the given order. Thanks! – AzureArmageddon Sep 03 '22 at 17:22

1 Answers1

3

Here your code is looping through the keys of a dictionary:

# If divisor is factor of num append corresponding rule_string to output
for divisor in ruleset:
    output += rule_string[divisor] if (num%divisor)==0 else ""

In Python 3.6 and later, the default dict implementation is an OrderedDict. (See Are dictionaries ordered in Python 3.6+?) That appears to not be the case in MicroPython 1.9.4 running on your calculator, so the keys aren't guaranteed to be in the order you specified when you created the rules dictionary.

Try making the ruleset an OrderedDict to see if you get the expected behavior, or change it to a list of tuples.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880