0

I'm doing a few practice problems( it's not HW learning on my own) and i ran into these two similar questions that are giving me a flag that the variable isn't defined. not sure what i'm doing wrong here.

They're both pretty much the same question so im sure solving one solves the other. Basically i asks to set a variable to a returning value.

#1 a program contains the following definition:
    def cube(num):
        return num * num *  num
   write a statement that passes the value 4 to this function and assigns its value to the variable 
   result

  my answer:

  result = cube(num)
  print('result',result)

  def cube(num):
      return num * num * num
  cube(4)

 flag:
 name 'num' is not defined

similarly

#2 write a function named get_first_name that ask the user to enter his or her first name, and 
 returns it

my answer:

result = get_first_name()
print('result',result)

def get_first_name():
    name = input('Enter name')
    return name
first_name()

flag:
name 'get_first_name' is not defined

not sure what i'm doing wrong... if i remove lines 1 and 2 and just start at the function definition it prints out the value fine. But then i didn't assign result to anything/ i cant check.

  **SOLUTION***

1. def main():
       result = cube(4)
       print('result:',result)
   def cube(num):
       return num * num * num

   main()


2. def main():
       result = get_first_name()
       print('result',result)
   def get_first_name():
       name = input('Enter name')
       return name

  main()
Carina
  • 3
  • 7
  • 1
    `result = cube(num)` num isn't defined in the global scope anywhere. Otherwise, you are using a function *before* you define it. – juanpa.arrivillaga Aug 25 '20 at 01:35
  • Yeah, the two problems are different, as jaunpa said. The duplicate applies to the latter. The former is additionally a mental typo: where should `num` come from when you call your function? – Andras Deak -- Слава Україні Aug 25 '20 at 01:36
  • im confused because i understand that you shouldnt mention a variable before you define it but i thought setting a variable to the returned value is different. isnt python supposed to see that i set the variable to a returned function value(result = cube(num)) then go to the function definition itself to see what the value is then return back to the print function and finish the execution? i had it work that way a few times but then it gives me an errors too so im not sure if its working the way i think it should if not how would i be able to assign the result variable to the returned value? – Carina Aug 25 '20 at 01:50
  • I put the argument: 4 into the parameter: num and was expecting it to get it from there. as it says i have to pass it through as an argument. so just a bit confused as how i would correct this. – Carina Aug 25 '20 at 01:53
  • Step 1: define the function (`def cube(num): ...`). Step 2: call the function (`cube(4)`). Step 3: the function returns a value (`return num ** 3` in the function). Step 4: you assign the result of the function call to a name (`result = cube(4)`). It can only ever work in this order. Python cannot see the future and predict that you're going to call it. – Andras Deak -- Слава Україні Aug 25 '20 at 01:57
  • What tutorial are you following, if any? – Andras Deak -- Слава Україні Aug 25 '20 at 01:57
  • first define function `def cube(num): ...` and later use `result = cube(4)` or `num = 4` and `result = cube(num)` – furas Aug 25 '20 at 12:12
  • *UPDATE* I solved my issue by creating a section for the main logic. You cant set a variable to a returned function value without it being inside a function itself. that's how i assume its working. if someone with more knowledge knows why this works the way it does please let me know because technically im doing what everyone said i couldnt... so idk why it can this way.I attached the solution to my problem above where it says *solution* that works perfectly. thank you for all your help. – Carina Aug 25 '20 at 18:45
  • @ andras deak I dont think this is the only way as i put above in my solution. the reason why that wasnt working for me is becuase if i implement it that way and would like to print something like print('result',result) if i put it at the end after the result = cube(4) in your order it wont print and i believe its because when it returns the value form return num **3 it doesnt continue to execute the rest. not sure if that exactly why, but maybe thats why return values are usually at the end instead of the middle of a function. If you know why that is that would be interesting to know. – Carina Aug 25 '20 at 18:58
  • I don't understand your confusion but I can try to explain why your solution works. When you execute `python your_script.py` python will first execute _the function definitions_ (creating the two functions and assigning them to the names `main` and `cube`, respectively), and then it executes `main()`. This is because python executes each block of code as it goes through your file. So first `main` is _defined_, then `cube` is _defined_, then `main` is _called_ with `main()`. When `main` is called it starts execution _inside the body of `main`_ which contains `result = cube(4)`. – Andras Deak -- Слава Україні Aug 25 '20 at 20:53
  • So by the time this line inside `main` gets executed, the function `cube` is already defined. Do you see how this is different from your original, where you were trying to _call_ a function before it was _defined_? – Andras Deak -- Слава Україні Aug 25 '20 at 20:53

0 Answers0