repl.it
@Phrancis/

class FizzBuzz()

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from collections import namedtuple

DivisorTextPair = namedtuple('DivisorTextPair', 'divisor text')

class FizzBuzz():
    def __init__(self):
        self.start_fizzbuzz()
    
    def start_fizzbuzz(self) -> None:
    	"""Controls the overall logic of a FizzBuzz program."""
        start_num, end_num = self.get_start_end_numbers()
        pair_list = self.get_divisor_text_pairs()
        self.print_numbers_and_texts(start_num, end_num, pair_list)
        
    def get_start_end_numbers(self) -> tuple:
    	"""Get user input for the start and end numbers for the FizzBuzz loop."""
        input_is_valid = False
        while not input_is_valid:
            start_num = input('Start Number? ')
            end_num =input('End Number? ')
            try:
                start_num = int(start_num)
                end_num = int(end_num)
                input_is_valid = True
            except ValueError:
                print('Invalid number input for start "{0}" or end "{1}".'.format(start_num, end_num))
        return start_num, end_num

    def get_divisor_text_pairs(self) -> list:
    	"""Ask user for a list of one or more divisor-text pairs for the FizzBuzz loop."""
        pair_list = []
        response = 'y'
        while response.lower() == 'y':
            input_is_valid = False
            while (input_is_valid == False):
                divisor = input('Divisor? ')
                try:
                    divisor = int(divisor)
                    input_is_valid = True
                except ValueError:
                    print('"{0}" is not a valid number.'.format(divisor))
            text = input('Text? ')
            pair = DivisorTextPair(divisor, text)
            pair_list.append(pair)
            response = input('Input Another Divisor (y/n)? ')
        return pair_list

    def print_numbers_and_texts(self, start_num: int, end_num: int, pair_list: list) -> None:
    	"""The FizzBuzz program loop to display output."""
        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)

def main():
    FizzBuzz()
    
if __name__ == '__main__':
	main()