Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当输入错误的用户输入时,设置循环以继续程序有困难_Python - Fatal编程技术网

Python 当输入错误的用户输入时,设置循环以继续程序有困难

Python 当输入错误的用户输入时,设置循环以继续程序有困难,python,Python,我对编程和Python相当陌生,我正在努力设置一个循环,以便在用户输入不正确时继续使用这个基本的名称生成器。我试图研究这个问题,但通常用整数之类的东西来找到答案,我很难翻译示例来执行我需要的程序。我不太确定我对if、ELSE和ELIF语句的使用是否正确。感谢所有能在这个话题上提供指导的人 import random def orcName(): # This is where the program will ask for male or female input from use

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

也许下面这样的构造会对您有所帮助:

def get_sex():
    sex = ''
    while sex is not 'Male' and sex is not 'Female':
          sex = input("What's your sex?")
    return sex

def get_name(sex):
    if sex is 'Male':
         name = ...
    else if sex is 'Female':
         name = ...
    return name

sex = get_sex()
name = get_name(sex)
print(name)

通过拆分代码逻辑并使用while语句继续查询,直到用户输入有效选项,我们改进了代码流。

如果使用python 2,则必须使用
raw\u 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!')

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

请选择男性或女性。记住大写计数
这是一个非常糟糕的用户体验的完美例子…我认为您不需要使用
break
语句,因为函数中没有
循环
。它给出了
SyntaxError:'break'外部循环
,删除
break
语句可以使代码运行良好,你能试试吗?无关:
break
在这里什么都不做,而且两个性别输入的名称完全相同。我以前尝试过设置循环,这就是为什么它们仍然存在的原因。只是我在发帖前的疏忽。不过,我想设置一个适当的循环,这样当用户输入与条件不匹配时,程序就会重复,而且我似乎无法正确设置。顺便说一下,任何新的python开发都应该使用python 3,所以不确定原始输入是否有助于响应。你们都提供了帮助,我可以看到我的代码可以以更合理的方式设置在哪里。在你们的指导下,我对它进行了重组,它按照我的意愿工作。