gpt4 book ai didi

python - 如何按顺序运行多个函数?

转载 作者:行者123 更新时间:2023-12-05 03:13:43 36 4
gpt4 key购买 nike

<分区>

对于那里有经验的编码人员来说,这应该是一个简单的...我正在编写一个程序,从困惑的输入中输出字谜(来自给定的列表)。我相信我的代码包含产生所需结果的所有必要函数,但我不知道如何让程序按顺序运行各个函数。示例输出是:

Please enter a jumbled word: lsitNe
Your words are:
silent
listen
enlist
tinsel

这是我目前的输出:

  Please enter a jumbled word: dff
Traceback (most recent call last):
File "/Users/edinnerman/Desktop/poop.py", line 53, in <module>
main()
File "/Users/edinnerman/Desktop/poop.py", line 5, in main
yay_anagrams = anagramlist(lword)
NameError: name 'lword' is not defined

如何重新格式化我的代码以在给定用户输入的情况下输出文本文件中列出的所有字谜?这是我的代码:

 #0: Create a main function to bring all functions together for the output.
def main():
textfile = open('words.txt', 'r').read()
dictionary_of_words = filename(textfile)
yay_anagrams = anagramlist(lword)
double_whammy = binary_search(fresh_list, ltextfile)
answer = output()

#1, 2: Take a filename as a parameter and ask user for word to unjumble. Lowercase all.
def filename(textfile):
ltextfile = textfile.lower()
scrambled_eggs = input("Please enter a jumbled word: ")
lword = scrambled_eggs.lower()

#3: Create an anagram list (already lowercase) for the jumbled word.
def anagramlist(lword):
if lword == "":
return([lword])
else:
yay_word = lword[1:]
first_letter = lword[0]
fresh_list = []
for mixed_word in filename(yay_word):
for pos in range(len(mixed_word) + 1):
fresh_list.append(mixed_word[:pos] + first_letter[0] + mixed_word[pos:])
return fresh_list

#4: Check if any words in anagram list match dictionary list.
def binary_search(fresh_list, ltextfile):
for eachword in fresh_list:
low = 0
high = len(list) - 1
while low <= high:
mid = (low + high)//2
item = list[mid]
if fresh_list == item:
return True
elif fresh_list < item:
high = mid - 1
elif fresh_list > item:
low = mid + 1
return False

#5: Print appropriate statements.
def output():
if fresh_list == True:
newlist = set()
for item in fresh_list:
newlist.add(item)
print("Your words are:/n", newlist)
if fresh_list == False:
print("Your word cannot be unjumbled.")
main()

请原谅我在这段代码中可能存在的一些小编码错误。

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com