2016-07-21 2 views
-3

私は、リストが最小から最大になるまでリストの2つの場所を交換するようにユーザに要求するコードを作成しています。だから、それは次のようになります。ユーザのソートリストを最小から最大にする

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] 

を私はこの部分を得ているが、私は現在のソートではなく、コードを実行するためにユーザーを取得する方法を見つけ出すことができません。ここで

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. 

は私のコードです:

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) 
+0

してください[編集]ので、あなたのインデントが正しいです。タブの代わりにスペースを使用してください。 –

+0

'list'をソートして' sorted_list'のような変数に設定し、何をしているのかを確認するだけでなく、 'list'が' sorted_list'と等しくなるまで各ユーザの入力をチェックしてください。 – davedwards

+2

'list'を変数として使用する識別子が誤解を招く可能性があります。代わりに 'lst'、' L'、 'my_list'などを使用してください。 – Tonechas

答えて

1

私はあなたのやや混乱の説明から理解できるものから、私はいくつかの良いコーディングで、この作業スクリプトは、Pythonを起動し、全体的なプログラミングをするとき、すべての初心者が学ぶべきで行って作られました。 2つの最初の小さな関数はコードの繰り返しを避けるために使用されています。このようにして、すべてのコードで長すぎるメイン関数も避けることができます。

また、最後の条件は、Pythonスクリプトを実行したときに発生するものです(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() 

質問や疑問は、お気軽にお問い合わせください。

(編集:より良いパターンをverify_index機能を修正、ユーザーTesselatingHeckerの提案)

+0

1つの小さなことは、 'list'が '1〜10'の範囲をハードコーディングする代わりに 'len()'を使用する代わりに異なる長さになるようにすることです。 – mitoRibo

+0

@rbiermanはい、確かです。また、コンフリクト/誤解を招く可能性のあるリスト変数の識別子を変更する可能性があります。ありがとう –

+0

最初の関数 'verify_index'は' if(true)return true else return false'の反パターンであり、 'return 1 <= number <= 10'を直接返すことができます。しかし、それを取り除いて 'if_exp(num1)'を 'if 1 <= num1 <= 10'に置き換えると、同じように動作し、より少数の文字にさえなります。 – TessellatingHeckler

関連する問題