我有两个函数cube,返回一个数字cubed和by_three,如果cube可以被3整除,我需要返回cube,否则返回false。这是我到目前为止(下面)。我一直得到“哎呀,再试一次.by_three(3)返回True而不是27”错误,如果你知道我做错了什么,或者可能是愚蠢的话,请帮帮忙。
def cube(number):
return number**3
def by_three(number):
return number%3==0
if bythree(number):
return cube(number)
else:
return false
答案 0 :(得分:0)
你的缩进遍布各处,但这可以做你想要的:
def cube(number):
return number**3
def by_three(number):
return number%3==0
def main(number):
return cube(number) if by_three(number) else False
if by_three(number)
为True cube(number)
将被调用并返回,否则只返回False。
您的代码在返回后无法访问,或者您的代码在返回无效的函数之外。在python中也没有false
它是大写F
`
答案 1 :(得分:0)
您需要第三种方法。
def cube(number):
return number**3
def by_three(number):
return number%3==0
def whattodo(number):
if by_three(number):
return str(cube(number)) #We must return one type, so we return string for both false and number
else:
return "false"
try:
print int(whattodo(input("Enter a number")) #We are trying to convert the string into an integer
except ValueError: #What if it is not a number? (In this case, it will be a string whose value is "false"
print "Your number is not divisible by three. Screw you"
答案 2 :(得分:0)
试试这个:
def cube(number):
return number**3
def by_three(number):
if number % 3 == 0:
return cube(number)
else:
return False