我试图创建一个将浏览数组的代码,当最终用户希望继续前进时,它会进入数组的开头。当在数组的开头并且用户希望向后移动时,它将进入数组的最后。虽然我能够从一个方面看,但我似乎无法连续走另一条路?当我进入P时,外观完美无缺,并会不断询问。虽然当我输入F时,循环在一次按下后停止。帮助我让F继续像p !!
#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 :(得分: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
答案 1 :(得分:0)
a = ["spam", "bacon", "baked beans"]
# This will never end.
for food in itertools.cycle(a):
print(a)
答案 2 :(得分: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)
看它是否符合您的要求。如果它确实很好,因为这里没有索引操作。如果没有,请发表评论。
答案 3 :(得分:0)
一些变化:
p
测试。通过添加elif
i
开始0
作为i
启动索引位置None
,以允许在第一个周期进行独特测试在打印元素之前的第一个增量索引之外的循环中。这会将代码配置为在用户在p
和n
之间切换时不重复元素,反之亦然。
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)