输入姓氏后,我想自动打印姓氏。
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 ))
答案 0 :(得分:0)
根据您的输入打印姓氏。如果它出现在first_name_list列表中,则获取索引并从带有该索引的last_name_list打印。
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 ))
答案 1 :(得分:0)
您可以创建一个将名字映射到姓氏的字典,方法是压缩两个列表并将结果序列传递给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")