在Python pigltin Trans中使用循环

2024-09-27 22:19:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我几个小时前就开始学习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。我怎么能把它放到一个循环中,一直问我同样的问题并显示结果,直到我关闭窗口?在


Tags: theinforinputlinetranslationinfileword
2条回答

您需要的是一个while循环。查看下面的函数gopig,看看如何使用它。在

请注意,对于piglin翻译,你可以做一些有趣的事情,像这样。。。首先使用deque,这是一个双链接列表。。。然后一次处理一行的列表理解。。。然后在一个函数中有while循环,这样就可以随时“pig”了。在

>>> def piglatinize(word):
...     from collections import deque
...     d = deque(word)
...     d.rotate(-1)
...     d.append('ay')
...     return ''.join(d)
... 
>>> def pigline(line):
...     return ' '.join(piglatinize(word) for word in line.strip().split())
... 
>>> def gopig():
...     while True:
...         line = input('Type a line to translate: ')
...         if not line: break
...         print pigline(line)
... 
>>> gopig()
Type a line to translate: "hello from a pig"
ellohay romfay aay igpay
Type a line to translate: ""
>>>

如果你想建立一个翻译dict,那么用下面的函数替换while循环中的上述函数。在

^{pr2}$

另外,如果您正在寻找一种简单的方法来查找是否有该单词,您可以在翻译dict上使用get。下面还使用您想要的*返回的“missing”值。在

>>> def pigfetch(word):      
...     return pigdict.get(word, '*'+word)
... 
>>> pigfetch('hello')
'ellohay'
>>> pigfetch('help')
'*help'
>>> piglate('help')
>>> pigfetch('help')
'elphay'

所以把它们放在一起,你会得到这个:

>>> def lookpig():
...     while True:
...         line = input('Type a word to find the translation: ')
...         if not line: break
...         print pigfetch(word)
...
>>> with open('words.txt', 'r') as f:
...     [piglate(line) for line in f]
...
>>> lookpig()
Type a word to find the translation: "hello"
ellohay
Type a word to find the translation: "goodbye"
*goodbye
Type a word to find the translation: ""
>>>

如果您已经有了一个基于文件的单词数据库,第一个单词是英语单词,第二个单词是piglin单词,那么您可以简单地执行以下操作:

>>> with open('lexicon.txt', 'r') as f:
...     pigdict.update(dict(line.strip().split() for line in f))
...
>>> lookpig()
Type a word to find the translation: "mars"
arsmay
Type a word to find the translation: "saturn"
*saturn
Type a word to find the translation: ""
>>>

不过,存储python dict而不是包含未翻译和翻译单词列的文本文件可能更容易。或者更好的是,使用一个实际的数据库。在

基本上把整个逻辑放在“while True”语句中。而且,如果像下面的例子一样输入“exit”,程序将停止(用“break”)。在

tDict = {}

with open("Lexicon.txt", "r") as infile:    
  for line in infile:
    s = line.split()
    tDict[s[0]] = s[1]

while True:
  wordIn = input("Enter the English word(s) to be translated (enter 'exit' to stop the program): ")

  if wordIn == "exit": break

  words = wordIn.split()
  for word in words:
    if word in tDict.keys():
      print(tDict[word])
    else:
      print("*" + word)

相关问题 更多 >

    热门问题