Python嵌套While循环语法错误:二级循环引发程序报错
解决Python二级循环的SyntaxError问题
你遇到的语法错误其实很直白——在Strong's二级循环的input()调用里漏写了闭合的右括号!这直接导致Python解析器无法正确识别后续的if语句,抛出了SyntaxError。除此之外,还有一个小细节会影响你“退出二级循环返回主循环”的需求,我一起帮你修正。
错误根源拆解
看你代码里的这一行:
strongs_inp = input("Get (D)efinition by Strong's Number, (E)xit: "
末尾缺少了一个),Python没法正确解析这个输入语句,自然会在紧接着的if行报错。
另外,strongs_conc变量的初始化位置不太对:你把它放在了主循环外面,第一次退出Strong's二级循环后,strongs_conc会被设为False,之后再进入Strong's菜单时,二级循环根本不会启动。得把它移到进入Strong's菜单的代码块内部,每次进入时重置为True。
修复后的完整代码
kjv = open('my file path /bible.txt','r').read() #split each verse into two item list [verse reference, scripture words]: scriptures_lst = [verse.split('\t') for verse in kjv.split('\n') if verse.split('\t')] #List of scripture references: references = [i[0] for i in scriptures_lst] #Word without scripture references: word = [i[1] for i in scriptures_lst] #Books of the bible books = [i.rstrip('0123456789: ') for i in references] #To remove duplicates from list by turning into a set, and then back into a list bible_books = list(set(books)) #Alphabetize the order of the books: alph_books = sorted(bible_books) #Search for verse function def scripture_search(): inp = input('verse? ') found = next(i for i in scriptures_lst if inp in i) return found continue_inquiry = True while continue_inquiry == True: #USER INPUT print("(S)earch, (N)ote, (B)ooks, (K)jv, Strong's \ Exhaustive (C)oncordance, (E)xit") request = input('What would you like to do? ') if request.upper() == 'SEARCH' or request.upper() == 'S': search = scripture_search() print(' - '.join(search)) #WRITE / APPEND VERSES FROM SEARCH TO A TEXT FILE #open the file in a+ - append plus mode #opens the file for appending to the end of the file (no overwrite) plus read mode versesearch_file = open('versesearch_file.txt', 'a+') versesearch_file.write(' - '.join(search) + "\n\n") versesearch_file.close() elif request.upper() == 'NOTE' or request.upper() == 'N': note = input('What note would you like to add? ') versesearch_file = open('versesearch_file.txt', 'a+') versesearch_file.write(note + "\n\n") versesearch_file.close() #print books of the bible list elif request.upper() == 'BOOKS' or request.upper() == 'B': print(alph_books) #print entire kjv elif request.upper() == 'KJV' or request.upper() == 'K': print(kjv) #start a new while loop for strong's exhaustive concordance elif request.upper() == "STRONG'S EXHAUSTIVE CONCORDANCE" or request.upper() == "C": # 每次进入Strong's菜单时重置循环标志 strongs_conc = True while strongs_conc == True: print("What would you like to do with Strong's?") # 补上缺失的右括号 strongs_inp = input("Get (D)efinition by Strong's Number, (E)xit: ") if strongs_inp.upper() == 'D': print('you selected D') elif strongs_inp.upper() == 'EXIT' or strongs_inp.upper() == 'E': strongs_conc = False # 退出二级循环,回到主循环 #exit from program elif request.upper() == 'EXIT' or request.upper() == 'E': continue_inquiry = False
关于返回主循环的说明
修复后,当你在Strong's二级循环中输入E或EXIT时,strongs_conc会被设为False,二级循环终止,程序会自动回到主循环的开头,等待你下一次的操作选择,完全符合你的需求。
内容的提问来源于stack exchange,提问作者Winget
相关产品推荐
相关产品推荐

