Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Variables python

Simple question, I like to declare my variables at the top of my programs below my function declarations so that it is a little easier to read. I run into problems when my variables are based on user input. I know I could do something like user_input = float and then when it was time to get the input later in the program I could redeclare it as user_input = float(input("")). But this runs into problems if I have variables declared up top that call a function using user_input as an argument, since user_input didn't actually have a value at run time. So how do I get the best of both worlds? I want to be able to declare all my variables up top and it not throw an error, then whenever the program actually needs the input I would reassign the value to the variable. Maybe the question wasn't so simple after all? Thank you guys

I have no answer because to me your question makes no sense. Obviously you would never use user_input in a function arguement if it has no value? However I may be misunderstanding you, so could you please give an actual coding example.

Try declaring the variable on top of your program in the form of a comment. That way it does not interfere with your program but you know its there and makes it easy to read. Hope this helps.

I would definetly not advise that, it's a bad idea to get used to bad practises, and programmers reading your code will find that weird.

That being said as i pointed out earlier, if you ever find yourself in a situation where this is neccesairy, its because you have done something else wrong.

1 Answer

Yeah sorry I can see how my question is confusing, however I have found the answer to my problem. Thank you for taking the time to comment though.