gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:38 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com