【问题标题】:Python: keep getting an error "unindent does not match any outer indentation level" [closed]Python:不断收到错误“未缩进与任何外部缩进级别不匹配”[关闭]
【发布时间】:2014-07-19 10:22:52
【问题描述】:

所以我在第一节 GCSE 计算机科学课上得到了一本 Python 小册子。一项任务要求我使用 and,or and not 运算符来提高此代码的效率:

one=int(input("Please Enter Number 1:"))
two=int(input("Please Enter Number 2:"))
three=int(input("Please Enter Number 3:"))
    if(one==two):
        if(two==three):
           print ("SNAP!")
    else:
        print ("The Do not all match")
    else:
        print("They do not all match")

但是我对 aand 或 and not 运算符一无所知。我试图找到正确的语法来使用。

然后我尝试使用此代码:

one=int(input("Please Enter Number 1:"))
two=int(input("Please Enter Number 2:"))
three=int(input("Please Enter Number 3:"))

    if one==two and two==three:
        print ("SNAP!")
    else:
        print ("The Do not all match")
    else:
        print("They do not all match")

但收到错误消息“未缩进与任何外部缩进级别不匹配”

我现在有点卡住了,因为我不知道这应该是什么意思,我知道这可能是一个简单的解决方法,但如果有人能指出我正确的方向,我将不胜感激,谢谢!

【问题讨论】:

  • 1) 将第四行的缩进减少一级。 2) 只使用一个else 子句。

标签: python syntax syntax-error


【解决方案1】:

您不能将两个 else 块放在一起,只需删除一个 else 并将您的代码块与输入对齐:

one = int(input("Please Enter Number 1:"))
two = int(input("Please Enter Number 2:"))
three = int(input("Please Enter Number 3:")) 

if one == two and two == three: 
    print ("SNAP!")
else:                               
    print("They do not all match")

【讨论】:

  • 什么是反对票?
  • 不是我。现在似乎已经消失的另一个答案根本没有帮助——你的很好,希望有用:)
  • 谢谢你的成功,考试委员会的书告诉我使用 2 'else' 我会通知我的学校!
【解决方案2】:

一个菜鸟的建议。

if one==two==three:
    print('SNAP!')
elif len(set([one,two,three]))==3:
    print('All three are different')
else:
    print('They do not all match')

【讨论】:

    猜你喜欢
    • 2010-12-15
    • 2017-08-12
    • 2018-02-08
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多