Review, suggestion etc?

Michał Jaworski swistakm at gmail.com
Thu Dec 17 12:16:05 EST 2020


> I think he's hinting at using a loop instead.
>
> while maritals != 'Yes' and maritals != 'No':
>    maritals = input('Married: Yes/No ?: ').title()

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"])

> 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).

> 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).

czw., 17 gru 2020 o 17:38 Michael Torrie <torriem at gmail.com> napisał(a):

> On 12/17/20 9:10 AM, Bischoop wrote:
> > Could you expand here, I rather don't know how I could do it different
> > way apart from if maritals == 'Yes' or maritals == 'No' or is it what
> > you meant?
>
> I think he's hinting at using a loop instead.
>
> while maritals != 'Yes' and maritals != 'No':
>     maritals = input('Married: Yes/No ?: ').title()
>
> I think I got the logical condition right. Sometimes those are tricky!
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list