Again code is marked right, but doesnt execute. :( What am I missing?


#1

Dear Pythoners,
again I ran into the code execution problem - the code is marked as correct, but simply doesn’t show any output on the console side :frowning:
what am I missing?

 def cube(number):
  return number ** 3
  
def by_three(number):
  if number % 3 == 0 :
    return cube(number) 
 

  else:
    return False
  
  
  number(15)
  
  print number
  print number ** 3
  print cube(number)

#2

i added format to your code, so the indent shows. Can you tell me that this is what your code looks like? (that the indent is right?)

well, function only execute when called, but you seemed to have placed your function call ( print cube(number)) inside your function body

there is no function call outside any function, so the functions never execute.


#3

Yes,
that’s what it looks like. I am not sure that understand what you mean…I thought the return is supposed to be part of the body, or is that wrong?


#4

yes, return needs to be inside the body of the function. But this is not what you are having a problem with

for a function to execute, a function needs be called.

Functions are a sort of task list. I can give you a task list (function), now you know the tasks which you need to do, have you actually done these tasks? Nope, i need to tell you to start executing the task list (function call)

now, functions are a bit more complex given function can call other functions

but you need one function call in the main part of your program, otherwise, none of the functions you wrote will execute


#5

Hm. I thought the function call would be number(15). But obviously, that’s not the case from what you said. can you give me a hint?


#6

trying to figure it out myself as well of course :slight_smile:


#7

I think I figured it out…


#8

you have two functions: cube and by_three, so that is what you should be calling

number is the function parameter, as such, you need to supply an argument at function call to satisfy the parameter (the parameter is just a placeholder until it gets its value from the argument at function call)


#9

Yes,
figured it out after reading through your former answer once again :slight_smile: thanks a lot!


#11

Hi again,
finally I can get back to the task. I was wondering if someone could tell me if my adjustments to the code are correct. The code, fortunately, gets executed now, but I’m wondering if my function call is right…here’s the adjusted code:

def cube(number):
return number ** 3

def by_three(number):
if number % 3 == 0:
return number / 3

else:
return False

print cube(15)
print by_three (15)

Isn’t the function call supposed to work without the ‘print’ before it? it doesn’t…hm.

Thank’s a lot for your help in advance.


#12

The functions do execute when you call them, even if you do not print the results. However, if you don’t include any print statements at all in your code, the results of calling the functions will not appear on the console.


#13

Ahh! Yeah, that makes sense…okay. Merci!


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.