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()