2016-04-13 17 views
-2

我試圖創建一個代碼,將瀏覽一個數組,並在最後和用戶希望前進時,它會進入數組的開始。而當數組開始時,用戶希望倒退時,它會進入數組的最後。儘管我能夠以一種方式看待,但我似乎無法連續走向另一條路?當我輸入P時,外觀完美並且會持續詢問。雖然當我輸入F時,循環在一次按下後停止。幫助我讓F繼續像P一樣!如何瀏覽Python中的數組?

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

#read file 
lines_in_file_array = input_file.read().splitlines() 


#appending the lines in a 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]) 

#Stop reading from file 
input_file.close() 

#This encrypts the message by turning each character into their individual 
#ascii values, adding 2, then converting those ascii values back to that 
#values character. 
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) 
    global temporary_array 


def navigation(): 
    # Index position 
    i = 0; 

    # Loop forever 
    while True: 


    # Get the user's input, and store the response in answer 
     answer = input("See Entry? P/F)?") 

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

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

      # and add to the index counter 
      i = i + 1 

     if answer.lower() == "p": 

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

      # and take away from the index counter 
      i = i - 1 


     # Otherwise leave the loop 
     else: 
      break 


encrypt() 
navigation() 
+0

請仔細閱讀https://stackoverflow.com/help/mcve,然後按[編輯]你的問題。 –

回答

1

你說 「如果f,這一點;如果p,這一點;否則突破;」 「else」語句僅適用於p,而不適用於f。

我想說的是,在這裏你檢查if answer.lower == 'p'不應該說if的一部分,它應該說elif

if answer.lower() == "f": 
    i = i + 1 

elif answer.lower() == "p": 
    i = i - 1 

else: 
    break 
0

使用itertools.cycle

a = ["spam", "bacon", "baked beans"] 

# This will never end. 
for food in itertools.cycle(a): 
    print(a) 
+0

這是完全正確的,只是一件事。它是一個無限迭代器。所以不會讓上面的代碼變得瘋狂?那反過來騎自行車呢? –

+0

當然會。這就是爲什麼我寫「這永遠不會結束」。你將不得不殺死處理。 –

0

看看下面的作品,說ls是列表。

from itertools import cycle 
rls = reversed(ls) 
z = zip(cycle(ls), cycle(rls)) 
while True: 
    choice = input("n/p: ") 
    n, p = next(z) 
    result = n if choice == "n" else p 
    print(result) 

看看它是否符合您的要求。如果這樣做很好,因爲這裏沒有索引操作。如果不是,請評論。

0

一些改動:

  1. 的其他人:打破只適用於p測試。通過在開始0添加elif
  2. 而不是開始的索引位置ii作爲None以允許在第一週期
  3. 一個獨特的測試在打印元件前的指數大於所述第一增量其他週期固定。這將代碼配置爲當用戶在pn之間切換時不重複元素,反之亦然。

    from sys import version_info 
    messagetext = ['one', 'two', 'three', 'four'] 
    
    def navigation(messagetext): 
        # Index position 
        i = None 
        py3 = version_info[0] > 2 #creates boolean value for test that Python major version > 2 
    
        # Loop forever 
        while True: 
         # Get the user's input, and store the response in answer 
         if py3: 
          answer = input('See Entry? P/F?') 
         else: 
          answer = raw_input('See Entry? P/F?') 
    
         # If the user entered lower case or upper case Y 
         if answer.lower() == 'f': 
          i = 0 if i == None else i + 1 
          print(messagetext[i % len(messagetext)]) 
          print("") 
    
         elif answer.lower() == 'p': 
          i = -1 if i == None else i - 1 
          # print the message 
          print(messagetext[i % len(messagetext)]) 
          print("") 
    
         # Otherwise leave the loop 
         else: 
          break 
    
    navigation(messagetext)