Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning a variable in while loop

I am encountering a problem for an assignment in my python3 class.

The code runs how is supposed to.
Ask the user the names of their dogs until they type ‘DONE’ and then tell them that each of their dogs are awesome by name.

However when I run the program you have to go through the input twice before you can get an output. I am new to this so I am sure the fix is quite simple.
Here is what I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def main():
    doggo_names = get_doggo_names()
    output_names(doggo_names)

def get_doggo_names():
    name = ''
    names = []
    while name != 'DONE':
        name = input('Name of doggo: ')
        if name != 'DONE':
            names.append(name)
    
    return names

def output_names(doggo_names):

    print()

    for names in get_doggo_names():
        print(names + ' ', end='is awesome.\n')

main()

>Solution :

Your code should be like this:

def main():
    output_names()

def get_doggo_names():
    name = ''
    names = []
    while name != 'DONE':
        name = input('Name of doggo: ')
        if name != 'DONE':
            names.append(name)

    return names

def output_names():

    for names in get_doggo_names():
        print(names + ' ', end='is awesome.\n')

main()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading