Confusing variable name in "Programming with Python : Repeating actions with loops" #568
Comments
Great point! Part of the motivation of the Python lesson is to teach best practices, including using descriptive variable names. How would you feel about using 'character' rather than c? |
|
In fact, Descriptive variable names are good in principle, but for loop indices, aren't shorter single letter variable names more the standard, especially if the value taken on by the loop variable is a scalar, i.e. integer ( But I also like I am all for descriptive variable names in an example such as:
In fact, I'd probably illustrate the example with something other than printing out a characters in a word). |
|
why doesn't |
I am not confused by any of it, but am just thinking about the beginning Python programmer who may have had some exposure to compiled languages (maybe as a first year student?) The choice of |
I like for |
I see. Then I'd be more inclined to either keep the example "as is" or change "char" to "letter" (or anything "more descriptive", rather than "c"). The reason being that if a learner had prior exposure to compiled languages, she or he should relatively quickly understand what is happening in Python. Please feel free to submit a PR to change Thinking of those with prior experience with C/Fortran... would it be helpful to check their understanding? Something like: > ## Identify variable
>
> In the following examples taken out of context, please identify all used variables:
>
> ~~~
> for char in word:
> print(char)
> ~~~
> {: .language-python}
>
> 1. `for`
> 2. `char`
> 3. `in`
> 4. `word`
>
> > ## Solution
> > ...
> {: .solution}
{: .challenge} |
Interesting challenge to ask students. In what context would such a question get asked? In the lesson? Or by the instructor? Or is there a "quiz" at the end of the lesson? Anyone with some prior language experience should figure out what is going on. But to reduce cognitive load (or just annoyance - I was bothered by this choice the minute I saw it!) , maybe better to use variable names that are not commonly used as type names in other languages. Of course, this can't be followed strictly - there will be inevitable conflicts. |
In the episode as a challenge.
Annoyance is not our concern. Cognitive load - is. I thought you were referring to a possible confusion that it's not |
No, I wasn't worried that someone might think the variable was I think I'll submit a PR. |
right, keywords can not be used as variable names, but I'm not sure we should include this information early in the lesson, if at all.
thanks! |
In the beginning lesson "Programming with Python : Repeating actions with loops', the following example is provided :
This choice of loop index variable
char
is a confusing choice, since it is also a the name of a type used in compiled languages such as C or Fortran. It would be much better to choose a name that is more useful as a loop variable, i.e. a single letter name. I propose the following:Or a more instructive option that doesn't conflict with compiled languages is
The text was updated successfully, but these errors were encountered: