1

So I'm writing a code that alphabetizes three words, but I'm trying to be cool, and actually alphabetize it (if both words start with h, it will go to the second letter). I'm basically a beginner, so nothing advanced I'm just using while loops. I got the code to work once, but then it stopped working. Out teacher in the direction said the function we write can't have a return, so here is my code.

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)
Ou Li
  • 21
  • 4
  • 1
    A function that doesn't return implicitly return `None`. – user202729 Feb 03 '18 at 07:28
  • What's the error message? – user202729 Feb 03 '18 at 07:28
  • You mean sort them? Just use `sorted` built in function. – user202729 Feb 03 '18 at 07:33
  • our teacher won't let us use sort. the error message was index out of range, and another time, it would just take input and not print anything – Ou Li Feb 03 '18 at 07:41
  • It would be good to read the original assignment. Your teacher might have a (maybe hidden) agenda and so you might not be allowed to to specific things. The shortest solution would be to use python sorting (https://docs.python.org/3.6/howto/sorting.html), but I assume you should learn to use if-clauses... – OBu Feb 03 '18 at 07:41
  • 1
    Then your teacher are giving non-realistic assignments. Try `eval('so'+'rt')`. – user202729 Feb 03 '18 at 07:41
  • @user202729: this is not unrealistic! It is more efficient to sort three items using if-clauses then to use sorting! The OP is just making the problem more difficult and then it would be better to use sorting... – OBu Feb 03 '18 at 07:44
  • @OBu Evidence please? I believe `sort` use insertion sort for small lists anyway. – user202729 Feb 03 '18 at 07:45
  • ...so you can save the function call ;-). But we are not only talking about programming efficiency if this is a teaching assignment, you'll learn something about decision trees and algorithm design while working on a solution. For solutions see https://stackoverflow.com/questions/4793251/sorting-int-array-with-only-3-elements – OBu Feb 03 '18 at 07:48
  • For the OP: Read [this](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) to learn how to debug your programs. – user202729 Feb 03 '18 at 07:51

2 Answers2

0

You need to make:

if list_1[0][i] == list_1[1][i] == list_1[2][i] 

as == is for comparison and = is for assignment in python. Also

i=int(i) 

is not needed as python can recognize 0 as an integer. We need not specify types for variables. Another major error is that you have to increment i at the end of the loop,i=i+1. The condition:

i < len(str1) < len(str2)< len(str3) 

is also wrong as this condition is true only if i< len(str1) and len(str1)< len(str2) and len(str2)< len(str3) which need not be the case everytime!Change your logic accordingly.. Using Python's easy built-in functions here is the code:

def printInOrder(str1, str2, str3):
    list_1 = [str1, str2, str3]
    list_1.sort()
    first = list_1[0]
    second = list_1[1]
    third = list_1[2]
    print first,second,third
printInOrder('hat','harry','ham')
yashdodeja
  • 11
  • 3
0

In my opinion you are contradicting your teachers intentions with your proposed solution. I strongly assume (s)he would like you to learn about sorting trees or decision trees - so you will only neede if-clauses. See sorting int array with only 3 elements for some solutions and further reading.

OBu
  • 4,977
  • 3
  • 29
  • 45