python - 访问在 while 循环中生成的函数的名称。(Python)
问题描述
下面是一个超级英雄的名字生成器。我希望能够将选择的名称添加到它说出去拯救世界的地方。有谁知道我该怎么做?
def superhero_name_generator():
print("Welcome to the superhero name generator:\nTo choose your name follow the instructions below")
first_part = input("Please enter the name of the city you grew up in. ")
second_part = input("Please enter the name of your pet or your favorite mythical creature (Ie. Dragon). ")
superhero_name = first_part + second_part
print("your superhero name is {}".format(superhero_name))
end = '-'
while end != 0:
superhero_name_generator()
print("If you want to generate another name please press 1 to quit press 0")
end = int(input())
else:
print("Go out and save the world")
解决方案
您必须从函数返回值。我还通过将其设为无限循环,稍微简化了您的循环break
。
def superhero_name_generator():
print("Welcome to the superhero name generator:\nTo choose your name follow the instructions below")
first_part = input("Please enter the name of the city you grew up in. ")
second_part = input("Please enter the name of your pet or your favorite mythical creature (Ie. Dragon). ")
superhero_name = first_part + second_part
print("your superhero name is {}".format(superhero_name))
return superhero_name
while True:
name = superhero_name_generator()
print("If you want to generate another name please press 1 to quit press 0")
if int(input()) == 0:
break
print("Go out and save the world,", name)
推荐阅读
- php - 如何在 Bootstrap DataTable 中显示数据
- c# - 使用 c# 在网格中加载大数据
- c# - 以编程方式设置复选框样式模板标签 WPF
- webpack - Webpack 无法本地覆盖 Chrome 中的文件
- google-cloud-endpoints - 在 Cloud Endpoints 中使用具有多个 API 版本的 x-google-endpoints OpenAPI 扩展
- rest-assured - RestAssured:如果响应正文具有额外值,如何使用 JSON 模式验证响应正文?
- java - 如何修复“sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException”
- javascript - HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized" - 使用 Post 请求发送图像
- java - 如何从java中的csv文件中的特定字段中删除逗号
- php - 嵌套数组的 Laravel 类型转换