Python 自动打印姓氏
输入名字后,我想自动打印姓氏Python 自动打印姓氏,python,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 )) 根据您的输入打印姓氏。如果它出现在第一个名称列表中,则使用索引从最后一个名称列表打印该索引 first_name_li
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 ))
根据您的输入打印姓氏。如果它出现在第一个名称列表中,则使用索引从最后一个名称列表打印该索引
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 ))
根据您的输入打印姓氏。如果它出现在第一个名称列表中,则使用索引从最后一个名称列表打印该索引
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")
通过压缩两个列表并将结果序列传递给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")
自动意味着?我的意思是,在用户输入他的名字后,程序会得到他的姓,并打印出他的名字。您希望如何存储您的姓和名?在列表中?你能举一个预期输入和预期输出的例子吗?我对python非常陌生,first_name=(“ata”,“amina”,“aarifa”)last_name=(“gowani”,“rajani”,“gowani”)print(“输入你的名字”)first_name=input()print(“你的姓是“,(last_name))#如何自动打印ata的姓氏?我的意思是,在用户输入他的名字后,程序将获得他的姓氏并使用姓氏打印。您希望如何存储您的姓氏和姓氏?在列表中?你能举一个预期输入和预期输出的例子吗?我对python非常陌生,first_name=(“ata”,“amina”,“aarifa”)last_name=(“gowani”,“rajani”,“gowani”)print(“输入你的名字”)first_name=input()print(“你的姓是“,(last_name))#我如何打印ata的姓氏)我不懂这个,除了关键错误:print(“您的姓名未注册”)这样,当用户输入的名字与
first_name
列表中的任何项目都不匹配时,脚本将打印友好的错误消息。当dict中不存在给定的键时,将引发keyror
异常。很高兴能提供帮助。如果您发现答案正确,是否可以将其标记为已接受t?(点击答案旁边的灰色复选标记。)我不懂这个,除了KeyError:print(“您的姓名未注册”)这样,当用户输入的名字与first_name
列表中的任何项目都不匹配时,脚本将打印友好的错误消息。当dict中不存在给定的键时,将引发keyror
异常。很高兴能提供帮助。如果您发现答案正确,是否可以将其标记为已接受t?(单击答案旁边的灰色复选标记。)