【问题标题】:Get username from previous def into another def with if statement使用 if 语句将用户名从前一个 def 获取到另一个 def
【发布时间】:2021-05-05 07:05:04
【问题描述】:

我有一些代码想在另一个 def 中再次使用输入的用户名,但它不会生成任何东西。我使用以下代码获得了一个用户名,没有问题(s_print 是一个慢速打印供参考):

def input_username():
    username = input()
    user = username
    s_print('Hello {}!'.format(user))
    return user

input_username()

然后我稍后在代码中有一个带有各种 if 语句的 def:

def options_input():
    if option == '1': etc.
    elif option == 'Bye':
        end_user = input_username()
        s_print('Goodbye {}!.'.format(end_user))
    else: etc.

options_input() 

我想让在 def input_username 中输入的用户名在 def options_input elif option == 'Bye' 中重新打印,但它只会生成没有错误代码/消息的空白,就像它在代码中不断循环一样。出了什么问题?

【问题讨论】:

  • 你能分享你的整个代码吗?

标签: python if-statement user-defined-functions


【解决方案1】:

input_username() 是一个函数

当您调用 input_username() 时,您需要将其保存在变量中以便以后使用

username = input_username()

然后在 options_input() 中

end_user = username

这一行:

end_user = input_username()

要求您再次输入用户名,它没有旧用户名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多