时间:2021-05-22
最近好多分享这个问题的代码,题目说的是用面向对象或者函数式编程,下面是PYTHON的实现示例
复制代码 代码如下:
#!/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()
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
最近在使用Python的过程中,发现网上很少提到在使用post方式时,怎么传一个数组作为参数的示例,此处根据自己的实践经验,给出相关示例:单纯的post请求:d
前言最近在使用Python的过程中,发现网上很少提到在使用post方式时,怎么传一个数组作为参数的示例,此处根据自己的实践经验,给出相关示例,下面话不多说了,来
Python+matplotlib进行鼠标交互,实现动态标注,数据可视化显示,鼠标划过时画一条竖线并使用标签来显示当前值。Python3.6.5,代码示例:im
python是解释型语言,本文介绍了Python下利用turtle实现绘图功能的示例,本例所示为Python绘制一个树枝,具体实现代码如下:python是解释型
问题如何使用list构造图邻接矩阵的方式Python代码示例#!/usr/bin/envpython#-*-encoding:utf-8-*-#author:L