1

I am fairly new to programming and Python and I am struggling with setting up a loop to continue this basic name generator when user input is incorrect. I tried researching this issue but usually find answers using integers and the like and am having difficulty translating examples to execute what I need the program to do. I am not quite sure if my using of the IF, ELSE, and ELIF, statements is even being used correctly. Thanks to anyone who can lend guidance on this topic.

import random


def orcName():
    # This is where the program will ask for male or female input from user.
    sex = input("What's your sex? ")


    # If user chooses Male the program will execute.
    if sex == 'Male':

        # This is where the list of male first names are stored.
        firstNameMale = ['Ughash', 'Spoguk', 'Truigig', 'Wudhagh', 'Wumkbanok', 'Hogug', 'Karrhig', 'Pargu',
                             'Xokuk', 'Furbog']

        # This is where the list of male last names are stored.
        lastNameMale = ['Opkagut', 'Zombilge', 'Grushnag', 'Gujarek', 'Igmut', 'Gorgo', 'Filge', 'XorokuShamob',
                            'Zurgha']
        # This is where the random function calls on the lists to assemble the name.
        firstName = random.choice(firstNameMale)
        lastName = random.choice(lastNameMale)
        print('Your name is', firstName, lastName)
        break

    # If user chooses Female the program will execute.
    elif sex == "Female":
            # This is where the list of female first names are stored.
        firstNameFemale = ['Ughash', 'Spoguk', 'Truigig', 'Wudhagh', 'Wumkbanok', 'Hogug', 'Karrhig', 'Pargu',
                               'Xokuk',
                               'Furbog']
        # This is where the list of female last names are stored.
        lastNameFemale = ['Opkagut', 'Zombilge', 'Grushnag', 'Gujarek', 'Igmut', 'Gorgo', 'Filge', 'XorokuShamob',
                              'Zurgha']
        # This is where the random function calls on the lists to assemble the name.
        firstName = random.choice(firstNameFemale)
        lastName = random.choice(lastNameFemale)
        print('Your name is', firstName, lastName)
        break

    # If user did not choose Male of Female program will execute.
    else:
        print('Please choose from Male or Female.  Remember capitalization counts!')
        break

orcName()
quamrana
  • 37,849
  • 12
  • 53
  • 71
HaydenDev
  • 23
  • 3
  • `Please choose from Male or Female. Remember capitalization counts` what a perfect example of extremely bad UX... – Marcin Orlowski Feb 17 '18 at 17:40
  • 3
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Feb 17 '18 at 17:42
  • I don't think you need `break` statement since there is no `loop` in the function. It gives `SyntaxError: 'break' outside loop`, removing `break` statements makes the code run fine, can you try that? – niraj Feb 17 '18 at 17:45
  • unrelated: `break` doesn't do anything here, and the names are the exact same for both gender inputs – OneCricketeer Feb 17 '18 at 17:47
  • Im aware, I had tried to set up a loop previously and that is why those are still there. Just an oversight on my part before posting. I want to set up a proper loop though, so that the program repeats if the user input doesn't match the conditions and I cant seem to set it up correctly. – HaydenDev Feb 17 '18 at 17:48
  • The names can be changed later to better reflect the sexes. This is just to practice and maybe create something functional for myself. – HaydenDev Feb 17 '18 at 17:49
  • I think I read question wrong, below answers may be helpful. – niraj Feb 17 '18 at 17:49

1 Answers1

1

You have to use raw_input if you use python 2.

Anyway if I get what you wanna do, this should look like the basic starting main program:

import random

if __name__ == '__main__':
    print("Hello, world")

    while 1:
        sex = raw_input("What's your sex? ")

        # If user chooses Male the program will execute.
        if sex == 'Male':

            # This is where the list of male first names are stored.
            firstNameMale = ['Ughash', 'Spoguk', 'Truigig', 'Wudhagh', 'Wumkbanok', 'Hogug', 'Karrhig', 'Pargu',
                             'Xokuk', 'Furbog']

            # This is where the list of male last names are stored.
            lastNameMale = ['Opkagut', 'Zombilge', 'Grushnag', 'Gujarek', 'Igmut', 'Gorgo', 'Filge', 'XorokuShamob',
                            'Zurgha']
            # This is where the random function calls on the lists to assemble the name.
            firstName = random.choice(firstNameMale)
            lastName = random.choice(lastNameMale)
            print('Your name is ' + firstName + ' ' + lastName)
            break

        # If user chooses Female the program will execute.
        elif sex == "Female":
            # This is where the list of female first names are stored.
            firstNameFemale = ['Ughash', 'Spoguk', 'Truigig', 'Wudhagh', 'Wumkbanok', 'Hogug', 'Karrhig', 'Pargu',
                               'Xokuk',
                               'Furbog']
            # This is where the list of female last names are stored.
            lastNameFemale = ['Opkagut', 'Zombilge', 'Grushnag', 'Gujarek', 'Igmut', 'Gorgo', 'Filge', 'XorokuShamob',
                              'Zurgha']
            # This is where the random function calls on the lists to assemble the name.
            firstName = random.choice(firstNameFemale)
            lastName = random.choice(lastNameFemale)
            print('Your name is ' + firstName + ' ' + lastName)
            break

        # If user did not choose Male of Female program will execute.
        else:
            print('Please choose from Male or Female.  Remember capitalization counts!')

Here I made an infinite loop until you enter Male or Female. Hope it helps.

shadowsheep
  • 14,048
  • 3
  • 67
  • 77
  • 1
    Any new python development should be using python 3, so not sure if raw_input helps – OneCricketeer Feb 17 '18 at 17:51
  • Thanks so much for the response. You all helped and I can see where my code could be set up in a more logical way. I restructured it following your guys' guidance and its working how I wanted. – HaydenDev Feb 17 '18 at 17:53