编写一个程序,提示用户输入名字和姓氏。然后程序将打印姓氏和名字。如果用户仅输入一个名称,则给出错误消息。请帮助我的if语句。非常感谢
name = str(input("Your first and last names? "))
gap = name.find(" ")
name_length = len(name)
first = name[0:gap]
last = name[gap + 1 : name_length]
if name_length is 2:
print("Name: ", last.upper(), ", ", first.capitalize(), sep = "")
else:
print("ERROR: only one name found")
这些就是我除了:
安格斯猪油草
您的名字和姓氏?姓名:SNODGRASS,安格斯
安格斯
您的名字和姓氏?错误:只找到一个名字
答案 0 :(得分:0)
尝试一下:
name = input("Your first and last name: ").split(" ")
if len(name) == 2:
print("Name: ", name[0].upper(), ", ", name[1].capitalize(), sep = "")
else:
print("ERROR: unknown number of names found!")
输出:
==== RESTART: C:/Users/...
Your first and last name: anGUS snodgrass
Name: anGUS, snodgrass
>>>
==== RESTART: C:/Users/...
Your first and last name: anGUS
ERROR: unknown number of names found!
>>>
答案 1 :(得分:0)
尝试此简单方法:
print("Enter your First and Last Name")
name = raw_input()
first,last = name.split()
print("Your FirstName:")
print(last.upper())
print("Your LasttName:")
print(first.lower())
答案 2 :(得分:0)
x=input("Your first and last names : ").split()
if(len(x)<=1):
print("name does not contain lastname or whole name is not mentioned ")
else:
print(x[1]+" "+x[0])