按字母顺序对多个字符串进行排序
alphabetically sorting multiple strings
我尝试编写的代码必须包含来自用户的提示和三个单独的字符串。当我要求用户在同一个提示中输入 3 个单词时,我能够使排序功能起作用,但是当我将输入分开时,排序功能不起作用。我对编码很陌生(第一周),所以如果不清楚,我深表歉意。
我应该编写一系列 Python 语句来提示用户输入三个字符串。然后,使用 'if' 语句,按字母顺序打印它们。
这是我目前的代码:
可能有人有什么建议吗?
谢谢!
word1 = input(str("Please enter a word of your choice:"))
word2 = input(str("please enter another word, but make sure the word does not start with the same letter:"))
word3 = input(str("Awesome! Ok, finally, enter one last word, again making sure not to use the same letter:"))
if (word1 == word2):
print("The words are the same")
else:
print("Nicely done, you read directions well!")
print("The words in alphabetical order are..")
接下来是什么?我在任何地方都找不到此信息。
您需要比较字符串并根据比较结果执行操作。使用来自 this resource to compare strings and the algorithm from this post 的字符串比较,您可以对三个字符串进行排序。
word1 = input(str("Please enter a word of your choice:"))
word2 = input(str("please enter another word, but make sure the word does not start with the same letter:"))
word3 = input(str("Awesome! Ok, finally, enter one last word, again making sure not to use the same letter:"))
if (word1 == word2):
print("The words are the same")
else:
print("Nicely done, you read directions well!")
print("The words in alphabetical order are..")
if(word1>word3):
tmp = word1
word1 = word3
word3 = tmp
if(word1>word2):
tmp = word1
word1 = word2
word2 = tmp
if(word2>word3):
tmp = word2
word2 = word3
word3 = tmp
print(word1)
print(word2)
print(word3)
如其他回复中所述,使用“if”语句肯定可以解决此问题。但是,如果您不局限于“if”语句,并且为了了解更多关于 Python 的信息,使用列表将以简洁明了的方式解决您的问题。请看下面的代码:
word1 = input(str("Please enter a word of your choice:"))
word2 = input(str("please enter another word, but make sure the word does not start with the same letter:"))
word3 = input(str("Awesome! Ok, finally, enter one last word, again making sure not to use the same letter:"))
if (word1 == word2 or word1 == word3 or word2 == word3):
print("The words are the same")
else:
print("Nicely done, you read directions well!")
word_list = [word1, word2, word3]
word_list.sort()
print("The words in alphabetical order are..")
for word in word_list:
print(word)
我尝试编写的代码必须包含来自用户的提示和三个单独的字符串。当我要求用户在同一个提示中输入 3 个单词时,我能够使排序功能起作用,但是当我将输入分开时,排序功能不起作用。我对编码很陌生(第一周),所以如果不清楚,我深表歉意。
我应该编写一系列 Python 语句来提示用户输入三个字符串。然后,使用 'if' 语句,按字母顺序打印它们。
这是我目前的代码: 可能有人有什么建议吗?
谢谢!
word1 = input(str("Please enter a word of your choice:"))
word2 = input(str("please enter another word, but make sure the word does not start with the same letter:"))
word3 = input(str("Awesome! Ok, finally, enter one last word, again making sure not to use the same letter:"))
if (word1 == word2):
print("The words are the same")
else:
print("Nicely done, you read directions well!")
print("The words in alphabetical order are..")
接下来是什么?我在任何地方都找不到此信息。
您需要比较字符串并根据比较结果执行操作。使用来自 this resource to compare strings and the algorithm from this post 的字符串比较,您可以对三个字符串进行排序。
word1 = input(str("Please enter a word of your choice:"))
word2 = input(str("please enter another word, but make sure the word does not start with the same letter:"))
word3 = input(str("Awesome! Ok, finally, enter one last word, again making sure not to use the same letter:"))
if (word1 == word2):
print("The words are the same")
else:
print("Nicely done, you read directions well!")
print("The words in alphabetical order are..")
if(word1>word3):
tmp = word1
word1 = word3
word3 = tmp
if(word1>word2):
tmp = word1
word1 = word2
word2 = tmp
if(word2>word3):
tmp = word2
word2 = word3
word3 = tmp
print(word1)
print(word2)
print(word3)
如其他回复中所述,使用“if”语句肯定可以解决此问题。但是,如果您不局限于“if”语句,并且为了了解更多关于 Python 的信息,使用列表将以简洁明了的方式解决您的问题。请看下面的代码:
word1 = input(str("Please enter a word of your choice:"))
word2 = input(str("please enter another word, but make sure the word does not start with the same letter:"))
word3 = input(str("Awesome! Ok, finally, enter one last word, again making sure not to use the same letter:"))
if (word1 == word2 or word1 == word3 or word2 == word3):
print("The words are the same")
else:
print("Nicely done, you read directions well!")
word_list = [word1, word2, word3]
word_list.sort()
print("The words in alphabetical order are..")
for word in word_list:
print(word)