我試圖創建一個代碼,將瀏覽一個數組,並在最後和用戶希望前進時,它會進入數組的開始。而當數組開始時,用戶希望倒退時,它會進入數組的最後。儘管我能夠以一種方式看待,但我似乎無法連續走向另一條路?當我輸入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()
請仔細閱讀https://stackoverflow.com/help/mcve,然後按[編輯]你的問題。 –