2016-03-23 3 views
0

y/nがユーザーによって入力されると、ユーザーは配列の次のエントリに移動するコードを作成しようとしています。それらが配列の最後に到達すると、ユーザーは配列の先頭に戻ります。以下同様です。これはこれまでの私のコードです。私はこれを多く研究しましたが、何をすべきかまだ分かりません。 私はアレイをループする際にヘルプを必要としているだけでなく、ユーザーの入力がどこで行うかを選択できるようにしています。Pythonで配列をナビゲートする方法は?

#declaring array names. 
longitude=[]; latitude=[]; messagetext=[];encryptions=[]; 
input_file = open('messages.txt', 'r') 


lines_in_file_array = input_file.read().splitlines() 


#appending the lines in a select file to select records. 
for line in lines_in_file_array: 
    record_array = line.split(',') 
    longitude.append(record_array[0]) 
    latitude.append(record_array[1]) 
    messagetext.append(record_array[2]) 

input_file.close() 

def encrypt(): 
    temporary_array=[] 
    for index in range(len(messagetext)): 
     x=messagetext[index] 
     x=([ord(character)+2 for character in x]) 
     codedx=''.join([chr(character) for character in x]) 
     temporary_array.append(codedx) 
     print(codedx) 

def navigation(): 
    continues=False 
    while continues == True: 




encrypt() 
print(messagetext) 
+0

'input("次の要素(y/n) "を参照)')を実行すると、 –

+0

ループ部分だけで助けが必要なのでしょうか、また入力を取り入れていますか?私は後者を見ませんが、私はあなたにその部分の助けを求めることはありません。 – David

+0

両方。私は今質問を編集します。 – Alev

答えて

0

ユーザーエントリを取得するには、次のようにinput()を使用します。

answer = input("See next entry ? (y/n)") 
if answer == "y": 
    #do the stuff 

をあなたのエントリは、サイクルにこの方法をitertools.cycleを使用することができます。

from itertools import cycle 

lines_in_file_array = input_file.read().splitlines() 

lines_in_file_array = cycle(lines_in_file_array) 

... 

その後、することができますループ無限:

for line in lines_in_file_array: 
    .... 

あなたが最後

0

に達するまで、私はあなたがナビゲートするのではなく、ユーザーの入力とナビゲーションしたい配列を確認していないあなたの配列の先頭に戻る:

def navigation(): 
    i = 0 
    continues = True 
    user = input("Continue?y/n") 
    if user == "y": 
     while continues: 
      print array[i] 
      i = (i+1)%len(array) 
      user = input("Continue?y/n") 
      if user != "y": 
       continues = False 
+0

継続は、キーワードです。 – David

+0

本当にありがとう@David – Whitefret

0

するとこれが可能にいったん配列を持てば、ほんの数行のコードで済ませます。

# Index counter 
i = 0; 

# Loop forever 
while True: 

    # Get the user's input, and store the response in answer 
    answer = input("See next entry (y/n)? ") 

    # If the user entered "y" or "Y" 
    if answer.lower() == "y": 

     # print the message 
     print(messagetext[i % len(messagetext)]) 

     # and increment the index counter 
     i = i + 1 

    # Otherwise leave the loop 
    else: 
     break 
関連する問題