def fizz_buzz(nums):
if nums % 5 == 0 and nums % 3 == 0:
print("FizzBuzz")
elif nums % 3 == 0:
print("Fizz")
elif nums % 5 == 0:
print("Buzz")
elif nums % 3 != 0 and nums % 5 != 0:
print(nums)
print(fizz_buzz(30))
问题:
我正在Google Colab中运行此代码。
答案 0 :(得分:1)
尝试一下:
def fizz_buzz(nums):
if nums % 5 == 0 and nums % 3 == 0:
return "FizzBuzz"
elif nums % 3 == 0:
return "Fizz"
elif nums % 5 == 0:
return "Buzz"
elif nums % 3 != 0 and nums % 5 != 0:
return nums
答案 1 :(得分:1)
请参见,fizz_buzz(30)
将返回30。但是,当您 打印 函数本身时,它一文不值。这是因为该函数不返回任何内容。解决此问题的一种方法是使用return
关键字。
您可以尝试以下方法:
def fizz_buzz(nums):
if nums % 5 == 0 and nums % 3 == 0:
print("FizzBuzz")
elif nums % 3 == 0:
print("Fizz")
elif nums % 5 == 0:
print("Buzz")
elif nums % 3 != 0 and nums % 5 != 0:
print(nums)
fizz_buzz(30)
def fizz_buzz(nums):
if nums % 5 == 0 and nums % 3 == 0:
return "FizzBuzz"
elif nums % 3 == 0:
return "Fizz"
elif nums % 5 == 0:
return "Buzz"
elif nums % 3 != 0 and nums % 5 != 0:
return nums
fizz_buzz(30)
答案 2 :(得分:0)
由于您没有从函数返回任何内容,因此只需使用参数调用该函数即可。下面是一个示例。
def fizz_buzz(nums):
if nums % 5 == 0 and nums % 3 == 0:
print("FizzBuzz")
elif nums % 3 == 0:
print("Fizz")
elif nums % 5 == 0:
print("Buzz")
elif nums % 3 != 0 and nums % 5 != 0:
print(nums)
fizz_buzz(30)
或者您可以返回某些内容并在函数外部进行打印。下面是一个示例。
def fizz_buzz(nums):
if nums % 5 == 0 and nums % 3 == 0:
return "FizzBuzz"
elif nums % 3 == 0:
return "Fizz"
elif nums % 5 == 0:
return "Buzz"
elif nums % 3 != 0 and nums % 5 != 0:
return nums
print(fizz_buzz(30))