在输入错误的用户输入时,很难设置循环以继续程序



我是编程和python的新手,我正在努力设置一个循环以在用户输入不正确时继续此基本名称生成器。我尝试研究这个问题,但通常使用整数等找到答案,并且很难翻译示例来执行我需要的程序要做的事情。我不太确定我使用的if,else和elif是否正确使用了语句。感谢任何可以在此主题的指导的人。

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

如果使用Python 2,则必须使用raw_input

无论如何,如果我得到了您想做的事情,这应该看起来像基本的开始主要程序:

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!')

在这里,我做了一个无限的循环,直到您进入男性或女性。希望它有帮助。

最新更新