且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我会得到“预期缩进的块"?当我尝试运行我的Python脚本时?

更新时间:2023-10-18 20:48:52

您可能会将制表符与空格混在一起.它看上去是 缩进的,但实际上不是.

You are probably mixing tabs with spaces. It looks indented but it really isn't.

您的代码给了我一个不同的错误:

Your code gives me a different error:

for ch in f:                                                  \
  ( translatedToken = english_hindi_dict[ch] )                \
    if (ch in english_hindi_dict) else (translatedToken = ch)
                                                        ↑

SyntaxError: invalid syntax

也许你是说:

for ch in f:
  if ch in english_hindi_dict:
    translatedToken = english_hindi_dict[ch]
  else:
    translatedToken = ch

也许您是说:

for ch in f:
  translatedToken = english_hindi_dict[ch] if ch in english_hindi_dict else ch

两者都应该运行良好,我希望第二个要比前一个更快

Both should run just fine, and I expect the second to be faster than the former

它们都可以优化为translated = str(english_hindi_dict.get(ch, ch) for ch in f),但这不是问题的重点.

They both can be optimized into translated = str(english_hindi_dict.get(ch, ch) for ch in f) but that's not the point of the question.