尝试对用户输入列表进行排序

时间:2016-10-08 15:00:44

标签: python loops sorting

我是python的新手。我创建一个脚本,允许用户输入三个名称,然后代码应打印名称,然后打印它们(按字母顺序)排序。我可以获取代码以返回已排序的单个字母,但不能将全名返回为字符串。我真的想要AS A LOOP !!!这就是我到目前为止所拥有的

print ('Enter three names, when done hit enter with a blank: ')

a = (raw_input('enter first name: ')) 
b = (raw_input('enter first name: '))
c = (raw_input('enter first name: ')) 

while not raw_input == "":
    print a + b + c
    break

srtd = a + b + c
name = srtd

for name in sorted(srtd):
    print name

2 个答案:

答案 0 :(得分:0)

print "Enter three names\n"
a = raw_input("Enter first name: ")
b = raw_input("Enter second name: ")
c = raw_input("Enter third name: ")
names = [a, b, c]
print a, b, c
for name in sorted(names):
    print name

答案 1 :(得分:0)

你可以在python中使用一个列表,它有一个很好的函数叫做sort你可以从列表代码中调用它

print ('Enter three names, when done hit enter with a blank: ')

a = (raw_input('enter first name: ')) 
b = (raw_input('enter first name: '))
c = (raw_input('enter first name: ')) 

my_list = [a,b,c]
my_list.sort()
print my_list

如果你想打印没有括号使用循环希望这有用