r/pythonhelp • • Oct 07 '21

HOMEWORK Easy: just started learning python. Can’t figure out how to assign the result to a variable. Don’t know what I did wrong here. Thank you to anyone willing to explain.

Post image
2 Upvotes

4 comments sorted by

2

u/[deleted] Oct 07 '21

In order to call a function, just treat it as if function_name(args) will be replaced with whatever that function returns. In other words, just like you could do variable = 1, or string = "foo", you can do result = function(argument).

When calling functions, you don't need to specify what argument is equal to do what, you can just do display_name('Janet', 'Liu'). Inside the function, firstname is whatever the first argument is, and lastname is whatever the second one is.

1

u/janetliy2001 Oct 07 '21

Thank you for the explanation about the call function which I now understand a bit better now. So I changed that as you suggested. However, I am still unsure how to do the second part. Where I have to # assign the result to a variable and print the variable. I think my method is wrong but what I did was create a new variable and made it equal to display_name. Then print that variable. But then it would just print <function display_fullname …

Display_name(‘Janet’, ‘Liu’)

My_name=display_name

Print(my_name)

Can you help explain this part. Thank you so much

2

u/[deleted] Oct 07 '21

Sure!

Right now you're assigning the actual function to my_name. What you want to do is call the function, and assign the result to it. You know you can call it just using display_name('Janet', 'Liu'), so all you have to do is use '=' to assign that to the variable.

You do this the same way you would if you were just assigning a string to a variable:
my_name = display_name('Janet', 'Liu')

2

u/janetliy2001 Oct 07 '21

Yayy I think I got it. The way you explained makes so much more sense than the way i did. I think i was just confused with what she meant by assigning result to an variable. I knew something was off if I make a variable directly equal to display_name twice.

But thank you so much again for taking your time to explain it to me! ☺️ My code was able to run and get the result that I wanted.