在代码中定义一个包含用户名和相应欢迎信息的字典,然后根据输入的用户名输出对应的欢迎信息。
示例代码:
user_welcome_msg = {
"John": "欢迎,约翰!",
"Alice": "欢迎,艾丽丝!",
"Bob": "欢迎,鲍勃!"
}
def greet_user(user_name):
if user_name in user_welcome_msg:
print(user_welcome_msg[user_name])
else:
print("抱歉,未找到该用户的欢迎信息。")
greet_user("John") # 输出:欢迎,约翰!
greet_user("Tom") # 输出:抱歉,未找到该用户的欢迎信息。
在上述示例代码中,定义了一个 user_welcome_msg
字典用于存储不同用户名的欢迎信息。greet_user
函数根据输入的用户名判断其是否在 user_welcome_msg
中出现过,如果有则输出对应的欢迎信息,否则提示未找到该用户的欢迎信息。