neolee / wop-community

29 stars 19 forks source link

IndentationError: unindent does not match any outer indentation level #106

Closed muse1171 closed 4 years ago

muse1171 commented 4 years ago

核对了好几遍就是不明白我的问题出在哪?核对了单词,没错。

#while True:
  try:
    x = int(input('Please enter a number: '))
    break
   except ValueError:
    print('Not a valid number. Try again...')

print('Your number is:', x)

File "<tokenize>", line 5
    except ValueError:
    ^
IndentationError: unindent does not match any outer indentation level
neolee commented 4 years ago

你的缩进有错误,except 子句需要和 try 对齐。

有个技巧,你可以把代码拷贝到 Visual Studio Code,保存为一个 *.py 文件,然后按 Control+Shift+P(macOS 的话是 Command+Shift+P),输入 Format Document,执行之,会自动帮你把代码样式处理好。

muse1171 commented 4 years ago
import time

class Bot:

    wait = 1

    def __init__(self):
        self.q = ''
        self.a = ''

    def _think(self, s):
        return s

    def run(self):
        time.sleep(Bot.wait)
        print(self.q)
        self.a = input()
        time.sleep(Bot.wait)
        print(self._think(self.a))

class HelloBot(Bot):

    def __init__(self):
        self.q = "Hi, what is your name?"

    def _think(self, s):
        return f"Hello {s}"
        class GreetingBot(Bot):

    def __init__(self):
        self.q = "How are you today?"

    def _think(self, s):
        if 'good' in s.lower() or 'fine' in s.lower():
            return "I'm feeling good too"
        else:
            return "Sorry to hear that"
            import random

class FavoriteColorBot(Bot):
    def __init__(self):
        self.q = "What's your favorite color?"

    def _think(self, s):
        colors = ['red', 'orange', 'yellow',
                  'green', 'blue', 'indigo', 'purple']
        return f"You like {s. lower()}? My favorite color is {random.choice(colors)}"

class Garfield:

    def __init__(self, wait=1):
        Bot.wait = wait
        self.bots = []

    def add(self, bot):
        self.bots.append(bot)

    def _prompt(self, s):
        print(s)
        print()

    def run(self):
        self._prompt("This is Garfield dialog system. Let's talk.")
        for bot in self.bots:
            bot.run()
            garfield = Garfield(1)

garfield.add(HelloBot())
garfield.add(GreetingBot())
garfield.add(FavoriteColorBot())
garfield.run()

截屏2020-07-18 15 04 01 这又是出现和上面类似的问题对吗?我用了上述老师教的方法,建立了文件,然后执行 Format Document,并没有改变什么。是我使用方法不对吗?google了一下最后还是不知道问题在哪。

neolee commented 4 years ago

你的 class HelloBot 那一行不该缩进,应该顶格写。

那个 Format Document 可以帮你把所有缩进对齐(不会出现前面那种有的行缩进一格有的两格有的四格的情况),但并不能帮你判断什么该缩进什么不该。

muse1171 commented 4 years ago

你的 class HelloBot 那一行不该缩进,应该顶格写。

那个 Format Document 可以帮你把所有缩进对齐(不会出现前面那种有的行缩进一格有的两格有的四格的情况),但并不能帮你判断什么该缩进什么不该。

这行错误修正了以后还是出现刚刚的报错, File "garfield.py", line 32 def init(self): ^IndentationError: expected an indented block

muse1171 commented 4 years ago
File "garfield.py", line 32
    def __init__(self):
    ^
IndentationError: expected an indented block
neolee commented 4 years ago

给出你修改后的完整代码,否则没法判断。

muse1171 commented 4 years ago
import time

class Bot:

    wait = 1

    def __init__(self):
        self.q = ''
        self.a = ''

    def _think(self, s):
        return s

    def run(self):
        time.sleep(Bot.wait)
        print(self.q)
        self.a = input()
        time.sleep(Bot.wait)
        print(self._think(self.a))

class HelloBot(Bot):

    def __init__(self):
        self.q = "Hi, what is your name?"

    def _think(self, s):
        return f"Hello {s}"
        class GreetingBot(Bot):

    def __init__(self):
        self.q = "How are you today?"

    def _think(self, s):
        if 'good' in s.lower() or 'fine' in s.lower():
            return "I'm feeling good too"
        else:
            return "Sorry to hear that"
            import random

class FavoriteColorBot(Bot):
    def __init__(self):
        self.q = "What's your favorite color?"

    def _think(self, s):
        colors = ['red', 'orange', 'yellow',
                  'green', 'blue', 'indigo', 'purple']
        return f"You like {s. lower()}? My favorite color is {random.choice(colors)}"
        class Garfield:

    def __init__(self, wait=1):
        Bot.wait = wait
        self.bots = []

    def add(self, bot):
        self.bots.append(bot)

    def _prompt(self, s):
        print(s)
        print()

    def run(self):
        self._prompt("This is Garfield dialog system. Let's talk.")
        for bot in self.bots:
            bot.run()
            garfield = Garfield(1)

garfield.add(HelloBot())
garfield.add(GreetingBot())
garfield.add(FavoriteColorBot())
garfield.run()
neolee commented 4 years ago

确定你的代码是上面这样的么?这里面好多缩进都错了啊……包括下面这些行:

class GreetingBot(Bot):
import random
class Garfield:
garfield = Garfield(1)

这几行都不该缩进,都应该顶格(而且它们前面都应该空行,会看起来更清晰),而且 import ... 那一行最好写在文件开始部分。

仔细检查下自己的代码,你对什么情况下需要缩进清晰吗?上面这几行的问题能理解还是不太能理解?

muse1171 commented 4 years ago

我在用nteract做完作业后直接复制粘贴到visulstudiocode里面,造成了很多地方没有顶格,但是自己都没有找出错误,看到系统报错,还以为出现在python def __init__(self):这行代码上面。我觉得是自己对于哪里需要缩进没有清晰的概念,所以才会找不出错误,平时直接在vc上面写代码,因为是抄写代码,加上vc软件会自动缩进,所以才不容易出错的,这次复制粘贴就出错了,自己也没察觉。不知道可以去看些什么来弥补不足。

neolee commented 4 years ago

缩进的规则其实挺简单的:

  1. 缺省是每行都顶格写;
  2. 出现冒号意味着之后的一行或者若干行需要缩进(并对齐),意思是缩进的这段代码是隶属于带冒号的那一行的,是那一行的下属;目前我们学过的带冒号需要缩进的就那几种,if ...: else: elif ...: while ...: for ... in ...: def ...: class ...: try: except ...: finally:
  3. 缩进可以多层,但每次缩进都要符合上述规则;
  4. Python 习惯上每层缩进都是缩进四格,可以用空格(space)也可以用制表符(tab);
  5. 在 Visual Studio Code 里可以用 Tab 按键来缩进一级,用 Backspace 来回退一级。

其他没啥了,多看正确的代码,多写。

muse1171 commented 4 years ago

好的,谢谢老师。

Neo Lee notifications@github.com于2020年7月18日 周六16:08写道:

缩进的规则其实挺简单的:

  1. 缺省是每行都顶格写;
  2. 出现冒号意味着之后的一行或者若干行需要缩进(并对齐),意思是缩进的这段代码是隶属于带冒号的那一行的,是那一行的下属;目前我们学过的带冒号需要缩进的就那几种,if ...: else: elif ...: while ...: for ... in ...: def ...: class ...: try: except ...: finally:;

  3. 缩进可以多层,但每次缩进都要符合上述规则;
  4. Python 习惯上每层缩进都是缩进四格,可以用空格(space)也可以用制表符(tab);
  5. 在 Visual Studio Code 里可以用 Tab 按键来缩进一级,用 Backspace 来回退一级。

其他没啥了,多看正确的代码,多写。

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/neolee/wop-community/issues/106#issuecomment-660447328, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGH4Y2TBOSFK2KPUCMMASBLR4FKBRANCNFSM4OXR6ZBQ .