我几个小时前就开始学习python了codeadmy.com网站我学会了如何制作一个pigltin翻译器,把第一个字母粘在末尾,然后再加上ay。我创造了很多conlangs,我想创建一个词典,我可以点击它搜索这个词,它会告诉我是否有这个词。我创建了一个(很好地使用其他人的教程)一个将在一个记事本文件中搜索这个词,如果它找到一个它会告诉我它是什么。但问题是,它允许我搜索一次然后关闭。有没有办法让它搜索,然后再问我是否想再搜索一次。我对Python不太流利,所以这听起来可能很愚蠢。哦,这是我目前为止的代码
tDict = {} # translation dictionary
with open("Lexicon.txt", "r") as infile:
for line in infile:
s = line.split()
tDict[s[0]] = s[1]
wordIn = input("Enter the English word(s) to be translated: ") #raw_input() in older Python
words = wordIn.split()
for word in words:
if word in tDict.keys():
print(tDict[word])
else:
print("*" + word) # the word doesn't have a translation
input()
我把input()放在末尾,这样我就必须在它关闭之前单击enter。我怎么能把它放到一个循环中,一直问我同样的问题并显示结果,直到我关闭窗口?在
您需要的是一个
while
循环。查看下面的函数gopig
,看看如何使用它。在请注意,对于piglin翻译,你可以做一些有趣的事情,像这样。。。首先使用deque,这是一个双链接列表。。。然后一次处理一行的列表理解。。。然后在一个函数中有while循环,这样就可以随时“pig”了。在
如果你想建立一个翻译dict,那么用下面的函数替换while循环中的上述函数。在
^{pr2}$另外,如果您正在寻找一种简单的方法来查找是否有该单词,您可以在翻译dict上使用
get
。下面还使用您想要的*
返回的“missing”值。在所以把它们放在一起,你会得到这个:
如果您已经有了一个基于文件的单词数据库,第一个单词是英语单词,第二个单词是piglin单词,那么您可以简单地执行以下操作:
不过,存储python dict而不是包含未翻译和翻译单词列的文本文件可能更容易。或者更好的是,使用一个实际的数据库。在
基本上把整个逻辑放在“while True”语句中。而且,如果像下面的例子一样输入“exit”,程序将停止(用“break”)。在
相关问题 更多 >
编程相关推荐