-3

So I'm making a code where it asks the user to to swap two places on the list until the list is from smallest to biggest. So it should look like this:

Hello:  Your current list is [6, 7, 8, 2 , 9, 10, 12, 15, 16, 17]
Please pick your first location ->   4
Please pick your second location ->  2
Your new list is [6, 2, 8, 7 , 9, 10, 12, 15, 16, 17]

I've gotten to this part but I am currently unable to figure out how to get the user to do the sorting and not the code.

Your list is not sorted: Please continue
Please pick your first location ->   1
Please pick your second location ->  2

Your new list is [2, 6, 8, 7 , 9, 10, 12, 15, 16, 17]
Please pick your first location ->   3
Please pick your second location ->  4

Your new list is [2, 6, 7, 8 , 9, 10, 12, 15, 16, 17]

Great job, thank you for sorting my list.

Here is my code:

list = [4,2,5,5,6,4,7,6,9,5]
print("Heres your current list", list)

print("Pick a location between 1 and 10")
num = int(input())
if num <= 10 and num >= 1:
    print("Please pick another location between 1 and 10")
    num1 = int(input())
    tempBox1 = list[num-1]
    tempBox2 = list[num1-1]
    list[num-1] = tempBox2
    list[num1-1] = tempBox1
    print("Your new list is", list)
martineau
  • 119,623
  • 25
  • 170
  • 301
myhstery
  • 9
  • 1
  • 4

1 Answers1

1

From what I could understand from your somewhat confusing explanation, I made this working script with some good coding conducts every beginner should learn when starting python and programming overall. The two first small functions are used to avoid code repetition, this way I can avoid aswell a too long main function with all the code.

Also, that last condition is something that happens when you run any python script (You can find a better explanation about here).

# Function to avoid code repetition
def verify_index(number):
    return 1 <= number <= 10

# Function to ask for the number indexes until they fit the list length
def input_numbers():
    while True:
        num1 = int(input("Pick a location between 1 and 10: "))
        num2 = int(input("Please pick another location between 1 and 10: "))
        if verify_index(num1) and verify_index(num2):
            return num1, num2

# List and variables defined locally here
def main_function():
    list = [2, 4, 5, 5, 5, 5, 5, 5, 9, 5]
    print("Heres your current list", list)
    num1, num2 = input_numbers()
    while True:
        print(num1,num2)
        temp = list[num1-1]
        list[num1-1] = list[num2-1]
        list[num2-1] = temp
        print("Your new list is now: ", list)
        if list == sorted(list):
            break
        num1, num2 = input_numbers()
    print("Congratulations! Your list is now sorted by your commands!")

# Code your script will execute once is run
if __name__ == '__main__':
    main_function()

Any question or doubt, feel free to ask.

(Edit: Fixing the verify_index function for a better pattern, suggestion by the user TesselatingHecker)

A Simões
  • 71
  • 1
  • 8
  • one small thing would be to allow `list` to be different lengths instead of hard-coding the `1-10` range and using `len()` instead – mitoRibo Jul 22 '16 at 00:59
  • @rbierman Yes, indeed. Also could change the list variable identifier that could be conflictual/misleading. Thank you – A Simões Jul 22 '16 at 01:02
  • The first function `verify_index` is an anti-pattern of `if (true) return true else return false` which could become `return 1 <= number <= 10` directly. But you could get rid of it and replace `if verify_index(num1)` with `if 1 <= num1 <= 10` and it would behave the same and even be fewer characters. – TessellatingHeckler Jul 22 '16 at 01:03