如何以这种方式按字母顺序列出?
How to list alphabetically in this way?
我必须在不使用列表排序方法的情况下对用户输入的名称进行排序。这是我到目前为止所拥有的,但在定义 'one' 'two' 和 'three' 时遇到问题。我需要程序检查每个字母以确保它真正按字母顺序排列。有人可以帮忙吗?
name1=str(input("Enter name #1: "))
name2=str(input("Enter name #2: "))
name3=str(input("Enter name #3: "))
one = name1[0].upper() + name1[1].upper() + name1[2].upper()
two = name2[0].upper() + name2[1].upper() + name2[2].upper()
three = name3[0].upper() + name3[1].upper() + name3[2].upper()
if one < two and two < three:
print("These names in alphabetical order are: ", name1, name2, name3)
elif one < two and three < two:
print("These names in alphabetical order are: ", name1, name3, name2)
elif two < three and three < one:
print("These names in alphabetical order are: ", name2, name3, name1)
elif two < one and one < three:
print("These names in alphabetical order are: ", name2, name1, name3)
elif three < two and two < one:
print("These names in alphabetical order are: ", name3, name2, name1)
else:
print("These names in alphabetical order are: ", name3, name1, name2)
提前致谢!
编辑 我的问题是定义 'one' 'two' 和 'three' 它需要 运行 通过输入中的所有字母。现在它 运行 是前三个字母,但如果我添加下一个字母并且只给出一个三个字母的名称,它就会出错。如果我使用 len 函数,它会告诉我它是一个整数
i need the program to go through each letter to make sure it is truly alphabetical.
这就是字符串比较的作用。您的代码的问题在于您将 one
、two
和 three
限制为输入字符串的前三个字母。相反,您应该将 整个 名称大写并进行比较。
name1 = input("Enter name #1: ") # no need for str(...)
... # same for name2, name3
one = name1.upper() # uppercase whole nam, not just first three letters
... # same for two, three
answer = "These names in alphabetical order are: " # don't repeat this X times
if one < two < three: # comparison chaining
print(answer, name1, name2, name3)
elif one < three < two:
print(answer, name1, name3, name2)
elif ...:
# a whole bunch more
else:
print(answer, name3, name2, name1)
您可以使用旧的排序算法
letters = [name1.upper(),name2.upper(),name3.upper()]
for i in range(len(letters)):
for j in range(i,len(letters)):
if letters[i] > letters[j]:
letters[i],letters[j] = letters[j],letters[i]
我必须在不使用列表排序方法的情况下对用户输入的名称进行排序。这是我到目前为止所拥有的,但在定义 'one' 'two' 和 'three' 时遇到问题。我需要程序检查每个字母以确保它真正按字母顺序排列。有人可以帮忙吗?
name1=str(input("Enter name #1: "))
name2=str(input("Enter name #2: "))
name3=str(input("Enter name #3: "))
one = name1[0].upper() + name1[1].upper() + name1[2].upper()
two = name2[0].upper() + name2[1].upper() + name2[2].upper()
three = name3[0].upper() + name3[1].upper() + name3[2].upper()
if one < two and two < three:
print("These names in alphabetical order are: ", name1, name2, name3)
elif one < two and three < two:
print("These names in alphabetical order are: ", name1, name3, name2)
elif two < three and three < one:
print("These names in alphabetical order are: ", name2, name3, name1)
elif two < one and one < three:
print("These names in alphabetical order are: ", name2, name1, name3)
elif three < two and two < one:
print("These names in alphabetical order are: ", name3, name2, name1)
else:
print("These names in alphabetical order are: ", name3, name1, name2)
提前致谢! 编辑 我的问题是定义 'one' 'two' 和 'three' 它需要 运行 通过输入中的所有字母。现在它 运行 是前三个字母,但如果我添加下一个字母并且只给出一个三个字母的名称,它就会出错。如果我使用 len 函数,它会告诉我它是一个整数
i need the program to go through each letter to make sure it is truly alphabetical.
这就是字符串比较的作用。您的代码的问题在于您将 one
、two
和 three
限制为输入字符串的前三个字母。相反,您应该将 整个 名称大写并进行比较。
name1 = input("Enter name #1: ") # no need for str(...)
... # same for name2, name3
one = name1.upper() # uppercase whole nam, not just first three letters
... # same for two, three
answer = "These names in alphabetical order are: " # don't repeat this X times
if one < two < three: # comparison chaining
print(answer, name1, name2, name3)
elif one < three < two:
print(answer, name1, name3, name2)
elif ...:
# a whole bunch more
else:
print(answer, name3, name2, name1)
您可以使用旧的排序算法
letters = [name1.upper(),name2.upper(),name3.upper()]
for i in range(len(letters)):
for j in range(i,len(letters)):
if letters[i] > letters[j]:
letters[i],letters[j] = letters[j],letters[i]