【发布时间】:2018-07-13 16:24:31
【问题描述】:
所以我正在编写一个按字母顺序排列三个单词的代码,但我想保持冷静,实际上是按字母顺序排列(如果两个单词都以 h 开头,它将转到第二个字母)。我基本上是一个初学者,所以没有什么先进的,我只是在使用 while 循环。我让代码工作了一次,但后来它停止工作了。方向out老师说我们写的函数不能有return,所以这是我的代码。
def printInOrder(str1, str2, str3):
i = 0
i = int(i)
list_1 = [str1, str2, str3]
while i < len(str1) < len(str2) < len(str3):
if list_1[0][i] = list_1[1][i] = list_1[2][i]:
i += 1
elif list_1[0][i] < list_1[1][i] < list_1[2][i]:
first = list_1[0]
second = list_1[1]
third = list_1[2]
elif list_1[0][i] < list_1[2][i] < list_1[1][i]:
first = list_1[0]
second = list_1[2]
third = list_1[1]
elif list_1[1][i] < list_1[0][i] < list_1[2][i]:
first = list_1[1]
second = list_1[0]
third = list_1[2]
elif list_1[1][i] < list_1[2][i] < list_1[0][i]:
first = list_1[1]
second = list_1[2]
third = list_1[0]
elif list_1[2][i] < list_1[0][i] < list_1[1][i]:
first = list_1[2]
second = list_1[0]
third = list_1[1]
else:
first = list_1[2]
second = list_1[1]
third = list_1[0]
first = str(first)
second = str(second)
third = str(third)
print(first,second,third)
【问题讨论】:
-
一个不隐式返回的函数返回
None。 -
错误信息是什么?
-
您的意思是对它们进行排序?只需使用
sorted内置函数即可。 -
我们的老师不会让我们使用排序。错误消息是索引超出范围,另外一次,它只需要输入而不打印任何内容
-
那么您的老师正在布置不切实际的作业。试试
eval('so'+'rt')。
标签: python function while-loop