HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

class fizzbuzz():

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classfizzbuzzstackoverflow

Problem

This is now an iterative review. The next iteration can be found here.

Yep. Fizzbuzz in Python.

I've been coding in Python for all of 90 minutes.

Thoughts?

Program Flow

Take a start_num and an end_num as user input

Ask for divisor/value pairs until user inputs a response that is not y

Iterate over the specified range, appending text in the order it was input where appropriate.

FizzBuzz.py

class divisor_text_pair():

    def __init__(self,divisor,text):
        self.divisor=divisor
        self.text=text

class fizzbuzz():

    start_num = int(input('Start Number? '))
    end_num =int(input('End Number? '))

    pair_list = []

    response = 'y'
    while response == 'y':

        divisor = int(input('Divisor? '))
        text = input('Text? ')
        pair = divisor_text_pair(divisor, text)
        pair_list.append(pair)
        response = input('Input Another Divisor (y/n)? ')

    def print_numbers_and_texts(start_num, end_num, pair_list):

        for num in range(start_num, end_num + 1):

            out_text = str(num)

            for pair in pair_list:

                if num % pair.divisor == 0:
                    out_text += ' '
                    out_text += pair.text

            print(out_text)

    print_numbers_and_texts(start_num, end_num, pair_list)

fizzbuzz()


Example Input/Output:

Start Number? 1
End Number? 10
Divisor? 2
Text? Fizz
Input Another Divisor (y/n)? y
Divisor? 3
Text? Buzz
Input Another Divisor (y/n)? n
1
2 Fizz
3 Buzz
4 Fizz
5
6 Fizz Buzz
7
8 Fizz
9 Buzz
10 Fizz

Solution

You do not need a class

The only method you have is static (it does not take self as argument) so you do not need a class.

The Pair class is also unnecessary because of tuple unpacking.

Please avoid over-complicating Fizz-Buzz.

Input handling in a separate function

Instead of top-level into the class.

Final solution

def print_numbers_and_texts(start_num, end_num, pair_list):
    for num in range(start_num, end_num + 1):
        print(num, end='')
        for divisor, text in pair_list:
            if num % divisor == 0:
                print(' ' + text)

def ask_pairs():
    while True:
        yield (int(input("Divisor? ")), input("Text? "))
        if input('Input Another Divisor (y/n)?') == 'n':
            break

print_numbers_and_texts(
    int(input('Start Number? ')),
    int(input('End Number? ')),
    list(ask_pairs())
)

Code Snippets

def print_numbers_and_texts(start_num, end_num, pair_list):
    for num in range(start_num, end_num + 1):
        print(num, end='')
        for divisor, text in pair_list:
            if num % divisor == 0:
                print(' ' + text)

def ask_pairs():
    while True:
        yield (int(input("Divisor? ")), input("Text? "))
        if input('Input Another Divisor (y/n)?') == 'n':
            break

print_numbers_and_texts(
    int(input('Start Number? ')),
    int(input('End Number? ')),
    list(ask_pairs())
)

Context

StackExchange Code Review Q#143207, answer score: 16

Revisions (0)

No revisions yet.