《Python数据分析技术栈》第01章 04 语法错误和异常处理(Syntax errors and exceptions)

本文主要是介绍《Python数据分析技术栈》第01章 04 语法错误和异常处理(Syntax errors and exceptions),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

04 语法错误和异常处理(Syntax errors and exceptions)

《Python数据分析技术栈》第01章 04 语法错误和异常处理(Syntax errors and exceptions)

Syntax errors are errors that may be committed inadvertently by the user while writing the code, for example, spelling a keyword wrong, not indenting the code, and so on. An exception, on the other hand, is an error that occurs during program execution. A user may enter incorrect data while running the program. If you want to divide a number (say, ‘a’) by another number (say, ‘b’), but give a value of 0 to the denominator (‘b’), this will generate an exception. The exceptions, which are autogenerated in Python and displayed to the user, may not lucidly convey the problem. Using exception handling with the try-except construct, we can frame a user-friendly message to enable the user to better correct the error.

语法错误是指用户在编写代码时可能无意中犯下的错误,例如拼错关键字、没有缩进代码等。异常则是在程序执行过程中发生的错误。用户在运行程序时可能会输入错误的数据。如果你想用一个数(如 “a”)除以另一个数(如 “b”),但给分母(“b”)的值是 0,这就会产生异常。在 Python 中自动生成并显示给用户的异常可能无法清楚地表达问题。通过使用 try-except 结构的异常处理,我们可以编写出用户友好的信息,使用户能够更好地纠正错误。

There are two parts to exception handling. First, we put the code that is likely to cause an error under a try clause. Then, in the except clause, we try to deal with whatever caused an error in the try block. We mention the name of the exception class in the except clause, followed by a code block where we handle the error. A straightforward method for handling the error is printing a message that gives the user more details on what they need to correct.

异常处理分为两个部分。首先,我们将可能导致错误的代码放在 try 子句中。然后,在 except 子句中,我们尝试处理 try 代码块中导致错误的代码。我们在 except 子句中提及异常类的名称,然后在代码块中处理错误。处理错误的一种直接方法是打印一条消息,向用户提供需要更正的更多细节。

Note that all exceptions are objects that are derived from the class BaseException, and follow a hierarchy.

请注意,所有异常都是从 BaseException 类派生出来的对象,并遵循一个层次结构。

A simple example of a program, with and without exception handling, is shown below.

下面是一个简单的程序示例,包括异常处理和无异常处理两种情况。

while True:try:n = int(input('Enter your score:'))print('You obtained a score of ', n)breakexcept ValueError:print('Enter only an integer value')

Same program (Without exception handling):

n=int(input('Enter your score:'))
print('You obtained a score of ',n)

The statement that is likely to cause an error in the preceding code is: int(input(‘Enter your score:’)). The int function requires an integer as an argument. If the user enters a floating-point or string value, a ValueError exception is generated. When we use the tryexcept construct, the except clause prints a message asking the user to correct the input, making it much more explicit.

在前面的代码中,可能导致错误的语句是:int(input(‘输入您的分数:’))。int 函数需要一个整数作为参数。如果用户输入的是浮点数或字符串值,就会产生 ValueError 异常。当我们使用 tryexcept 结构时,except 子句会打印一条信息,要求用户更正输入内容,这样就更加明确了。

这篇关于《Python数据分析技术栈》第01章 04 语法错误和异常处理(Syntax errors and exceptions)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/620444

相关文章

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

Python从零打造高安全密码管理器

《Python从零打造高安全密码管理器》在数字化时代,每人平均需要管理近百个账号密码,本文将带大家深入剖析一个基于Python的高安全性密码管理器实现方案,感兴趣的小伙伴可以参考一下... 目录一、前言:为什么我们需要专属密码管理器二、系统架构设计2.1 安全加密体系2.2 密码强度策略三、核心功能实现详解

Python Faker库基本用法详解

《PythonFaker库基本用法详解》Faker是一个非常强大的库,适用于生成各种类型的伪随机数据,可以帮助开发者在测试、数据生成、或其他需要随机数据的场景中提高效率,本文给大家介绍PythonF... 目录安装基本用法主要功能示例代码语言和地区生成多条假数据自定义字段小结Faker 是一个 python

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.