Review, suggestion etc?
Bischoop
Bischoop at vimart.net
Thu Dec 17 12:47:54 EST 2020
On 2020-12-17, MichaĆ Jaworski <swistakm at gmail.com> wrote:
>
> Exactly. I would go even further and make it a reusable function. Eg.
>
> def prompt_choices(prompt, choices):
> choices = set(c.lower() for c in choices)
> while value := input(f"{prompt} {choices}:").lower() not in choices:
> pass
> return value
>
> Or without := operator:
>
> def prompt_choices(prompt, choices):
> value = None
> choices = set(c.lower() for c in choices)
> while value not in choices:
> input(f"{prompt} {choices}:").lower()
> return value
>
> That way you can use it as follows:
>
> marital = prompt_choices("Married", ["yes", "no"])
>
Oh man that really not like they teach in tutorials.
These are the examples at which you look and think: moment, I need a few sec
to follow :-)
>> So in other words I shoudn't nest functions like in
>> changes(), add_people() etc but keep
>> everything in one functions.
>
> Simply move those functions outside of their "hosting" functions and
> call them as they are. Also, many of them won't be needed anymore as you
> introduce some generic helper functions (e.g. prompt_choices).
>
Honestly I've done it like that because I thought that would be more elegant
way to have how_old(), marriage() etc hosted in add_people() but I'm glad I've
done this time because learn a bit, I had quite a problem to keep people
argument storing and returning in every function, I see it was unecessary
as I know now but another experience.
>> Now I'll learn OOP as you said and then try to made this program again
>> with OOP from scratch.
>
> Recommend polishing the procedural approach a bit more too, though. Like
> reducing code repetition. Looking into how data is stored, eg. can fields
> be stored in CSV columns instead of JSON? OOP can be overwhelming at the
> very beginning. For instance it can take some time learning what should be
> an object and what shouldn't. You definitely can start adding e.g.
> dataclasses because they are more like structures (e.g. struct in C
> mentioned earlier).
>
Right I keep it in mind. Yes, the storing you mention I'll have to
improve, even the json I've done sucks because I've noticed it storing
everything in one column lol I thought that json library handles it
itself but apparently I have to tell him about it to insert key,values
into different columns.
--
Dzieki
More information about the Python-list
mailing list