自动打印姓氏

2024-10-05 14:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在输入名字后自动打印姓氏

first_name = ("ata","amina","aarifa")
last_name = ("gowani","rajani","gowani")
print ("enter your first name")
first_name = input()
print ("your last name  is", (last_name ))

Tags: nameinputyouris名字firstlastprint
2条回答

根据您的输入打印姓氏。如果它出现在第一个名称列表中,则使用索引并使用该索引从最后一个名称列表打印

first_name_list = ("ata","amina","aarifa")
last_name_list = ("gowani","rajani","gowani")
print ("enter your first name")
first_name = input()
if first_name in first_name_list:
    index = first_name.index(first_name)
    last_name = last_name_list[index]
    print ("your last name  is", (last_name ))

通过压缩两个列表并将结果序列传递给dict构造函数,可以创建一个将名字映射到姓氏的dict:

first_name = ("ata","amina","aarifa")
last_name = ("gowani","rajani","gowani")
mapping = dict(zip(first_name, last_name))
print("enter your first name")
first_name = input()
try:
    print("your last name  is", mapping[first_name])
except KeyError:
    print("your name is not registered")

相关问题 更多 >