[英]Having trouble translating strings
I've decided to start on a project over spring break, taking characters from a webcomic and translating text as if that character was saying it. 我决定在春假期间开始一个项目,从网络漫画中获取字符并翻译文本,就好像该字符在说。 I've managed to get it to work well for one character, but there's a slight problem.
我设法使它适合一个角色,但是有一个小问题。
def meulin():
replace = {'EE':'33', 'ee':'33'}
originalText = input('Input text -> ')
while True:
for i, j in replace.items():
if i in originalText:
newText = originalText.replace(i,j)
print(newText.upper())
else:
print(originalText.upper())
originalText = input('Input text (type "quit" to end program.) -> ')
if originalText in ('quit', 'end', 'exit', 'stop', 'q'):
sys.exit('Program ended.')
When I ran PyScripter's debugger, it told me that after getting input, the program starts at the for i, j in replace.items():
line, skips the if statement completely and goes to the else statement, then goes to the if statement. 当我运行PyScripter的调试器时,它告诉我,输入后,程序从
for i, j in replace.items():
行中开始,完全跳过if语句并转到else语句,然后转到if语句。 So instead of just posting 因此,不只是发布
CH33SE
CH33SE
it'll post 它会发布
CHEESE
起司
CH33SE
CH33SE
I could just remove the else statement completely, but then it wouldn't post the original text at all. 我可以完全删除else语句,但是它根本不会发布原始文本。 Any suggestions would be appreciated.
任何建议,将不胜感激。
for i, j in replace.items():
if i in originalText:
newText = originalText.replace(i,j)
print(newText.upper())
break
else:
print(originalText.upper())
The break
statement means the loop will stop after the first substitution. break
语句意味着循环将在第一次替换后停止。 Python allows an else
clause on a for
loop which will execute only if the loop is not stopped by a break
statement. Python允许在
for
循环上使用else
子句,该子句只有在break
语句未停止循环时才执行。
The reason you saw the output twice is because you have two items in your dict. 您两次看到输出的原因是因为您的字典中有两个项目。 I think you are looking to print the original text only when none of the substitutions match.
我认为您希望仅在所有替换都不匹配时才打印原始文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.