How to browse through array in Python?
我正在尝试创建一个将浏览数组的代码,当最后用户希望继续前进时,它会进入数组的开头。 当在数组的开头并且用户希望向后移动时,它将进入数组的最后。 虽然我能够从一个方面看,但我似乎无法连续走另一条路? 当我进入P时,外观完美无缺,并会不断询问。 虽然当我输入F时,循环在一次按下后停止。 帮我让F继续像p !!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #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() |
你说"如果f,这个;如果是p,这个;否则就会破坏;""else"语句仅适用于p,而不是f。
我所说的是你检查
1 2 3 4 5 6 7 8 | if answer.lower() =="f": i = i + 1 elif answer.lower() =="p": i = i - 1 else: break |
一些变化:
在第一个增量之外的循环中,在打印元素之前的索引。 这会将代码配置为在用户在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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) |
看看以下是否有效,比如说
1 2 3 4 5 6 7 8 | 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) |
看它是否符合您的要求。 如果它确实很好,因为这里没有索引操作。 如果没有请评论。
使用
1 2 3 4 5 | a = ["spam","bacon","baked beans"] # This will never end. for food in itertools.cycle(a): print(a) |