This article describes how to use python to implement the FizzBuzzWhizz issue on the internet. For more information, see the code for sharing this issue, this topic is about object-oriented or functional programming. The following is an example of PYTHON implementation.
The code is as follows:
#! /Usr/bin/python
# Encoding: utf8
'''
The Game of "FizzBuzzWhizz"
Author: wang.jiankui89@gmail.com
Mobile: 130-2199-5152
'''
Import sys
Class teacher:
Def _ init _ (self, student_num ):
Self. student_num = student_num
Def gameStart (self, numList ):
For I in range (1, self. student_num + 1 ):
Stu = student (I)
Print stu. answer (numList)
Class student:
Def _ init _ (self, my_num ):
Self. my_num = my_num
Def judgeMod (self, numList ):
ModRes = ""
For num in numList:
If self. my_num % num = 0:
ModRes + = rule [num]
Return modRes if modRes else self. my_num
Def judgeContain (self, first_num ):
ConRes = ""
If str (first_num) in str (self. my_num ):
ConRes = rule [first_num]
Return conRes
Def answer (self, numList ):
ConRes = self. judgeContain (numList [0])
Return conRes if conRes else self. judgeMod (numList)
Def getOps ():
'''Parse options '''
If len (sys. argv )! = 4:
Print "use as fizzBuzzWhizz. py [0-9] [0-9] [0-9]"
Sys. exit ()
Else:
First_num = int (sys. argv [1])
Second_num = int (sys. argv [2])
Third_num = int (sys. argv [3])
Return first_num, second_num, third_num
Def main ():
First_num, second_num, third_num = getOps ()
Global rule
Rule = {first_num: "Fizz", second_num: "Buzz", third_num: "Whizz "}
Student_num = 100
Tea = teacher (100)
Tea. gameStart (first_num, second_num, third_num ))
If _ name _ = "_ main __":
Main ()