Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请通过代码说明try…except…else中的else子句的作用 #114

Open
Sogrey opened this issue Feb 20, 2020 · 0 comments
Open

请通过代码说明try…except…else中的else子句的作用 #114

Sogrey opened this issue Feb 20, 2020 · 0 comments

Comments

@Sogrey
Copy link
Owner

Sogrey commented Feb 20, 2020

while True:
    try:
        x = int(input('请输入x:'))
        y = int(input('请输入y:'))
        value = x / y
        print('x / y is',value)
    except Exception as e: # 发生异常时执行
        print('不正确的输入:',e)
        print('请重新输入')
    else:  # 未发生异常时执行
        break

try…except…else语句中的except子句中的代码会在发生异常时执行,而else子句中的代码会在try…except之间的代码正常执行完后执行,也就是当代码执行正确时执行。

@Sogrey Sogrey added this to 编程应用 in Python QAs Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Python QAs
编程应用
Development

No branches or pull requests

1 participant