The return command in my functions has stopped working correctly halfway through the course, it doesn't return anything now.
I made it to Problem Set 6, every function I have written until now has worked fine. Now my functions won't return the value they're supposed to. To make it as easy as possible to see the problem, here is a function I am writing now that won't return what it is supposed to: def return_an_input_value(x): return x return_an_input_value(5) When I run this in my terminal, I don't get anything back except: logout [Process completed] As if it completed it's task, but it didn't. Now if I slightly change the function so that it prints the value x before returning and run this in my terminal... def return_an_input_value(x): print x return x return_an_input_value(5) I get: 5 logout [Process completed] The 5 is being printed out as a result of the print command. But return x should be returning a 5 as well. My output for this redundant function SHOULD be: 5 5 logout [Process completed] Can someone explain to me what's going on? It's as if my preferences have changed or something. I've tried restarting/shutting down my comp, restarting TextWrangler (the text editing program I've been using until now that has worked fine), reloading the .py file and rerunning it over and over. I can't figure it out. Can someone figure this out?
I'm running old programs from problem set 4 I wrote that return values for definitions that are called and they are now not working. ughh
idk try print return_an_input_value(5)
That prints it out but what I need (which I see I didn't make explicit) is for my function to return the value I need it to so that I can use that value in other places. I have a lot of functions I have to put together and I need the return values to come out otherwise my programs that use functions won't work.
By the way... when I run return_an_input_value(5) in IDLE. It works fine, the return value 5 comes out, but when I "Run in Terminal" from TextWrangler, it doesn't return the value 5.... I guess something is wrong with my TextWrangler?
if my command prints 5 it means function returns 5 try a = return_an_input_value(5) and print a
Yes. Mostly likely it's how the Python interpreter is parsing and interacting with TextWrangler. Your code seems fine.
Tomas.A, I see where you were going now. Thanks. Both commands you suggested work. Sooo what should I do now?
i think everything is ok tell me what doesn't work
Ok, so I guess the function is really returning the value, I merely don't see it being returned. I just find it unusual because up until today, every function I called would show me what value, list, or dictionary is being returned. If it's not doing this now I guess I may have unintentionally changed some settings in TextWrangler sometime today but I don't know how. The automatic printing of whatever was being returned after I called a function is what I would use to help me debug a program up until today, I guess I will try printing the function call from now to do debugging until I figure out how to get the automatic printing of the return value upon calling a function property back. Thanks!
to make you happier in other languages you always have to print if you want to know value which is returned from function or just any variables value :D
oh gosh, that's going to take some getting used to. thanks Tomas.A!
Your code seems to be running fine. Your original function: def return_an_input_value(x): return x is returning x- you just do not see it happening. Since you have not coded for anything else to happen, the program does the simple task you 'asked' it to, returns 5, and is finished. With any function, in order to see whether it is working correctly, store what is returned under some variable name and work with that variable name as a test of how your function behaved (i.e. what it returned). for instance (using your example above): def return_an_input_value(x): return x number_five=return_an_input_value(5) print number_five*5 You should see a 25 print to screen. If you get an error, you can back track to see what went wrong. The same is true for any function that is written to return a value. If you call that function, and it is written correctly, it will return the appropriate value- but that is all it will do. Returning is not the same as printing, and so you will notice nothing in just calling the function and having it return a value.
Join our real-time social learning platform and learn together with your friends!