python - 当输入错误的用户输入时,难以设置循环来继续程序

标签 python

我对编程和 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!')

这里我做了一个无限循环,直到你输入“男性”或“女性”。希望对您有所帮助。

关于python - 当输入错误的用户输入时,难以设置循环来继续程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844055/

相关文章:

python - 使用列名编写 csv 并读取从 Pyspark 中的 sparksql 数据框生成的 csv 文件

python - 从第一个非数字元素中提取所有元素

python - z3python : converting string to expression

python - 将 C++ 指针作为参数传递给 Cython 函数

python - 检查 python 中的条件是否匹配

python - 如何为 REST Api 功能设置 Python

python - linux : during downloading anaconda jupyter i got these errors

python - 用python检查它是否是一个幻方

python - 根据 Pandas 中的条件连接多个值将单个列呈现为新列

python - mac 运行 deepfakes - faceswap ' faceswap.py' 出现错误