[Tutor] Question about code readability and good coding practise

dn PyTutor at DancesWithMice.info
Sun Feb 7 01:48:27 EST 2021


On 07/02/2021 18.25, Manprit Singh wrote:
> Dear Sir ,
> 
> Just need your suggestions :
> 
> Consider a problem where I have to make a program that allows the user to
> give integer inputs 10 times from the keyboard and then put all these 10
> integers into a python list.
> Writing like this would be considered readable and good code ?
> 
> var =  [int(input("Enter number")) for i in range(10)]

List comprehensions are concise and powerful. Very useful to those
in-the-know, but can be quite mystifying (and therefore error-prone) to
'beginners'.

The code does not include a gap between the input-prompt and the
user-entered data.

Is it a coding matter or perhaps more for user experience (UX): there is
no extra indication to the user that (s)he is expected to respond -
better practice would include adding a question-mark or colon.


At the specification level, the idea of asking for ten numbers without
distinguishing between them, seems odd - and asking for user-confusion
or user-introduced errors, eg do I put "width" before "height" or
vice-versa?


At the execution level, how do you handle data-entry errors, eg pressing
(or on the previous line, double-pressing) Enter? What about someone
typing "t" or "y" instead of "5"?


Combining the last two topics, how will the code be able to advise the
user which piece of data is at-fault?


If there is a fault, which is caught, how will the code AND the user
know where to re-start the amorphous list of 10 inputs?
PS they are not 'just' "inputs" (per question wording), the code
requires that they be (by Python definition and the user's machine's
implementation) integers!
-- 
Regards,
=dn


More information about the Tutor mailing list