4 ms·
In the Python templates, what’s with the variable “initialisations” —- e.g. t = 0 in the light-speed one? I’ve seen similar stuff in my eldest’s computing home
by dghf 5y ago
In the Python templates, what’s with the variable “initialisations” —- e.g. t = 0 in the light-speed one? I’ve seen similar stuff in my eldest’s computing homework: is it an accepted Python idiom that I’ve missed somehow?
- Jtsummers 5y agoIt looks like they've made the minimum amount of code that will execute (but not pass the tests). By using a variable instead of just hardcoding 0 into the return they give you a specific thing to assign to and as long as you leave that return t bit alone at the end your code will work. Though, in that case, the solution is so trivial that t is completely unnecessary.
- tremon 5y agoI presume it is to prevent a NameError if you run the template without adding any code.
- PolarizedPoutin 5y agoYeah the other two replies explain why we used code stubs that would at least run but not pass. Don't think it's an idiom (at least not one that I've seen). It's definitely not the nicest code, especially for simpler problems that are really one-liners but we figured lots of people would just click "submit" on just the code stubs.