游戏规则
遇到3的倍数输出“Fizz",遇到5的倍数输出”Buzz",遇到15的倍数输出“FizzBuzz"(本文参考褚则伟老师的课程,可自行搜索2333)
传统实现方法
1.代码
from __future__ import print_function
import torch
def fizz_buzz_encode(i):
if i % 15 == 0: return 3
elif i % 3== 0 : return 1
elif i % 5 == 0 : return 2
else : return 0
def fizz_buzz_decode(a , prediction):
return [str(a), "fizz","buzz","fizzbuzz"][prediction]
def helper(b):
print( fizz_buzz_decode(b , fizz_buzz_encode(b)) )
for c in range (1,16):
helper (c)
2.输出
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
3.关注点&其它用法
def fizz_buzz_decode(a , prediction):
return [str(a), "fizz","buzz","fizzbuzz"][prediction]
其它用法
def ceshi (d):
return [str(d),"zhao"