try / except语句突出显示“ except”的语法错误

时间:2019-10-14 06:16:58

标签: python

我有一个非常简单的try / except块,基本上可以将变量“ password”从用户输入中定义为整数。

这可能是一个愚蠢的问题,但是我尝试环顾四周,却找不到解决方案。

Blob

但是,当我尝试运行此python shell时,它出现了一个语法错误,突出显示了'except'...

2 个答案:

答案 0 :(得分:0)

password = ...之后删除新行。壳牌希望除外块而不是新行。另外,您的括号也不完整。

try:
    password = int(input('Bob should have issued you with your 4-digit numerical password, please enter it here: '))
except ValueError:
    print("Please do not type letters or symbols!!")

答案 1 :(得分:0)

有几个问题:

  1. 您必须使用类似这样的比较器:类型(密码)不是整数
  2. 预先定义密码变量,否则将在您的while语句中将其识别
    password=""
    try:
        password = int(input('Bob should have issued you with your 4-digit numerical password, please enter it here: '))


    except ValueError:
        print("Please do not type letters or symbols!!")

    while type(password) is not int:
        try:
            password = int(input('Bob should have issued you with your 4-digit numerical password, please enter it here: '))

        except ValueError:
            print("Please do not type letters or symbols!!")

    print('Complete, we have your password.')