# 【Python CheckiO 题解】Fizz Buzz

CheckiO 是面向初学者和高级程序员的编码游戏，使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务，从而提高你的编码技能，本博客主要记录自己用 Python 在闯关时的做题思路和实现代码，同时也学习学习其他大神写的代码。

CheckiO 官网：https://checkio.org/

CheckiO 题解系列专栏：https://itrhx.blog.csdn.net/category_9536424.html

CheckiO 所有题解源代码：https://github.com/TRHX/Python-CheckiO-Exercise

## 题目描述

【Fizz Buzz】：“Fizz buzz” 是一个单词游戏，我们将教会机器人学会关于除法的一些知识。你将要使用 python 写一个方法，方法接收一个整数，如果这个整数既可以整除 3 也可以整除 5，则返回"Fizz Buzz"；如果这个整数只能整除 3，则返回"Fizz"；如果这个整数只能整除 5，则返回"Buzz"；其他情况则返回传入的这个整数。

【输入】：一个整数值

【输出】：返回的结果（字符串）

【前提】：0 < number ≤ 1000

【范例】

checkio(15) == "Fizz Buzz"
checkio(6) == "Fizz"
checkio(5) == "Buzz"
checkio(7) == "7"


## 代码实现

# Your optional code here
# You can import some modules or create additional functions

def checkio(number: int) -> str:
# It's main function. Don't remove this function
# It's using for auto-testing and must return a result for check.

if number % 3 == 0 and number % 5 == 0:
return 'Fizz Buzz'
elif number % 3 == 0:
return 'Fizz'
elif number % 5 == 0:
return 'Buzz'
else:
return str(number)

# Some hints:
# Convert a number in the string with str(n)

# These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
print('Example:')
print(checkio(15))

assert checkio(15) == "Fizz Buzz", "15 is divisible by 3 and 5"
assert checkio(6) == "Fizz", "6 is divisible by 3"
assert checkio(5) == "Buzz", "5 is divisible by 5"
assert checkio(7) == "7", "7 is not divisible by 3 or 5"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")


## 大神解答

#### 大神解答 NO.1

def checkio(number):
#It's main function. Don't remove this function
#It's using for auto-testing and must return a result for check.
if number%3!=0 and number%5!=0:
return f'{number}'
return f"{(number%3==0)*'Fizz'}{(number%3==0 and number%5==0)*' '}{(number%5==0)*'Buzz'}"


#### 大神解答 NO.2

def checkio(number: int) -> str:
if not number % 3 and not number % 5:
return "Fizz Buzz"

elif not number % 3 or not number % 5:
return "Fizz" if not number % 3 else "Buzz"

else:
return str(number)


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客