当前位置:首页 > 教育培训 >

python运行不出结果(python运行不出结果也不报错)

来源:原点资讯(www.yd166.com)时间:2023-06-04 23:21:02作者:YD166手机阅读>>

1.异常

python发生错误时,会创建一个异常对象,如果编写了处理该异常的代码,程序将继续运行,如果没有编写,程序将停止,并显示一个traceback,其中包含异常的报告。

1.ZeroDivisionError异常

print(5/0)#这样做会出现异常 ZeroDivisionError Traceback (most recent call last) <ipython-input-2-244cf93eeabc> in <module> 3 #使用try-except代码块处理异常 4 #####1.处理异常 ----> 5 print(5/0) ZeroDivisionError: division by zero 2.使用try-except代码块处理异常

try: print(5/0) except ZeroDivisionError: #出现异常后如何做 print('you can not divide by zero') you can not divide by zero 3.使用异常避免崩溃

当输入5和0时,程序崩溃,运行不出结果

print('give me two numbers, and i will divide them') print("enter 'q' to quit") while True: first_number = input('\nfirst number: ') if first_number == 'q': break second_number = input('\nsecond number: ') if second_number == 'q': break answer = int(first_number)/int(second_number) print(answer) give me two numbers, and i will divide them enter 'q' to quit first number: 5 second number: 0 --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-1-e2f10f797953> in <module> 10 if second_number == 'q': 11 break ---> 12 answer = int(first_number)/int(second_number) 13 print(answer) ZeroDivisionError: division by zero 4.解决办法

print('give me two numbers, and i will divide them') print("enter 'q' to quit") while True: first_number = input('\nfirst number: ') if first_number == 'q': break second_number = input('\nsecond number: ') if second_number == 'q': break try: answer = int(first_number)/int(second_number) except ZeroDivisionError: #出现异常后如何做 print('you can not divide by zero') else:#不出现异常时怎么办 print(answer) give me two numbers, and i will divide them enter 'q' to quit first number: 5 second number: 0 you can not divide by zero first number: 5 second number: 2 2.5 5.处理FileNotFoundError异常

原因:查找的文件可能在其它地方,文件名不正确或这个文件根本不存在

with open('tmp.txt') as tmp: contents = tmp,read() FileNotFoundError Traceback (most recent call last) <ipython-input-1-963f3942d4a5> in <module> 1 #####5.处理FileNotFoundError异常 2 #原因:查找的文件可能在其它地方,文件名不正确或这个文件根本不存在 ----> 3 with open('tmp.txt') as tmp: 4 contents = tmp,read() FileNotFoundError: [Errno 2] No such file or directory: 'tmp.txt' ================================================ #解决办法 try: with open('tmp.txt') as tmp: contents = tmp.read() except FileNotFoundError: print("对不起,该文件不存在") 对不起,该文件不存在 6.分析文本

使用split()函数将文本中包含的单词创建为一个列表

try: with open('tmp.txt') as tmp: contents = tmp.read() except FileNotFoundError: print("对不起,该文件不存在") else: words = contents.split(): num_words = len(words) print('the file tmp.txt ' 'has about ' str(num_words) 'words') 7.分析多个文件,可使用函数处理

#先计算一个文件包含多少单词 def count_words(filename): try: with open(filename) as tmp: contents = tmp.read() except FileNotFoundError: print("对不起,该文件不存在") else: words = contents.split(): num_words = len(words) print('the file tmp.txt ' 'has about ' str(num_words) 'words') filename = 'alice.txt' count_words(filename) ======================================= #通过循环统计多个文件包含多少单词 def count_words(filename): --snip-- filenames = ['alice.txt', 'a.txt', 'b.txt', 'c.txt'] for filename in filenames: count_words(filename) 8.希望程序发生异常时一声不吭

def count_words(filename): try: with open(filename) as tmp: contents = tmp.read() except FileNotFoundError: pass#使用pass语句处理 else: words = contents.split(): num_words = len(words) print('the file tmp.txt ' 'has about ' str(num_words) 'words') filename = 'alice.txt' count_words(filename) 2.存储数据

模块json能够将简单的python数据结构存储在文件中,并在程序再次运行时加载该文件,也可以在python程序之间分享数据

1.json.dump,存储内容,包括两个实参,要存储的数据以及可用于存储数据的文件对象

import json numbers = [1, 2, 3, 4, 5] filename = 'numbers.json' with open(filename, 'w') as f_obj: json.dump(numbers, f_obj)#将数字列表存储到文件json.dump中

python运行不出结果,python运行不出结果也不报错(1)

image.png

2. json.load #加载文件

import json filename = 'numbers.json' with open(filename) as f_obj: numbers = json.load(f_obj) print(numbers) [1, 2, 3, 4, 5] 3.保存和存储用户生成的数据

对于用户生成的数据,使用json保存很好用,首次运行程序时被提示输入数据,再次运行程序就会记住他

import json username = input('请输入姓名: ') filename = 'username.json' with open(filename, 'w') as f_obj: json.dump(username, f_obj) print('我们将记住你的选择' username) 请输入姓名: 大明 我们将记住你的选择大明 ====================================== ###打开文件时向用户发出问候 import json filename = 'username.json' with open(filename) as f_obj: username = json.load(f_obj) print('欢迎回来 ' username) 欢迎回来 大明 5.重构

代码能够正确运行,但可做进一步改进,将代码划分为一系列完成具体工作的函数,这个过程叫重构

#假设有一个函数 import json def greet_user(): filename = 'username.json' try: with open(filename) as f_obj: username = json.load(f_obj) except FileNotFoundError: username = input('请输入姓名: ') with open(filename, 'w') as f_obj: json.dump(username, f_obj) print('我们将记住你的选择' username) else: print('欢迎回来 ' username) greet_user() 欢迎回来 大明 #重构greet_user(),让它不执行那么多任务,将获取存储用户名的代码移到另一个函数中 import json def get_stored_username(): #如果存储了用户名就读取它 filename = 'username.json' try: with open(filename) as f_obj: username = json.load(f_obj) except FileNotFoundError: return None#函数要么返回预期值,要么返回None else: return username def greet_user(): username = get_stored_username() if username:#如果获取了用户名,就打印一条欢迎用户回来的消息,否则提示用户输入用户名 print('欢迎回来 ' username) else: username = input('请输入姓名: ') filename = 'username.json' with open(filename, 'w') as f_obj: json.dump(username, f_obj) print('我们将记住你的选择' username) greet_user() 或者将greet_user()单独放在一个函数中 import json def get_stored_username(): #如果存储了用户名就读取它 filename = 'username.json' try: with open(filename) as f_obj: username = json.load(f_obj) except FileNotFoundError: return None#函数要么返回预期值,要么返回None else: return username def get_new_username(): username = input('请输入姓名: ') filename = 'username.json' with open(filename, 'w') as f_obj: json.dump(username, f_obj) print('我们将记住你的选择' username) return username def greet_user(): username = get_stored_username() if username:#如果获取了用户名,就打印一条欢迎用户回来的消息,否则提示用户输入用户名 print('欢迎回来 ' username) else: username = get_new_username() print('我们将记住你的选择' username) greet_user()

栏目热文

python代码不能持续运行(python代码没错但无法运行)

python代码不能持续运行(python代码没错但无法运行)

文 | xybaby 出处 | cnblogs服务器程序员最怕的就是程序crash,不过有时候程序没有crash,但是“...

2023-06-04 23:12:08查看全文 >>

python代码正确但是不运行

python代码正确但是不运行

这节课我们讲要了解一个新的知识:异常处理。假设我们写一个程序,计算两个数相除的商,程序运行后除数输入0。再假设程序中要求...

2023-06-04 23:11:12查看全文 >>

python点击运行没反应(python的idle点了之后没反应)

python点击运行没反应(python的idle点了之后没反应)

安装Python后,它自带一个编辑器IDLE,但是使用几次之后出现启动不了的情况,可做如下操作。Windows操作系统下...

2023-06-04 23:23:02查看全文 >>

python复制的代码不能运行(复制的python代码无法运行)

python复制的代码不能运行(复制的python代码无法运行)

全文共3733字,预计学习时长7分钟python最近火了,大红大紫那种。PYPL(编程语言受欢迎程度) 四月官方榜单宣布...

2023-06-04 22:47:28查看全文 >>

python代码有时能运行有时不能(python代码正确但是运行没有结果)

python代码有时能运行有时不能(python代码正确但是运行没有结果)

Python是一种强大的编程语言,但是在编写Python程序时,程序崩溃是一个常见的问题。程序崩溃会导致程序无法继续执行...

2023-06-04 23:09:30查看全文 >>

python代码没错但运行不出来(python写完代码怎么运行)

python代码没错但运行不出来(python写完代码怎么运行)

#父与子的编程之旅#与小卡特一起学Python#第十四课一#终于到了对象这一章节,集前面学习的大成,尝试着一点一点的理解...

2023-06-04 22:41:24查看全文 >>

python代码点了运行没用(python代码运行后不动怎么办)

python代码点了运行没用(python代码运行后不动怎么办)

本文只适合python初学者,老鸟可以先飞走。turtle是python标准库之一,可以用来简单地实现画图功能。在使用t...

2023-06-04 22:52:25查看全文 >>

python代码总是无法运行

python代码总是无法运行

本章节我们来讲述一下Python的编码规范,通过详细对代码编写规则以及命名规范等进行介绍。1.编写规则Python采用P...

2023-06-04 23:17:00查看全文 >>

python无法运行(python程序运行不了怎么办)

python无法运行(python程序运行不了怎么办)

在Python中导致程序无法运行的有2种情况。1;语法错误,这是致命错误,需要重新查看代码进行修改,比如:缩进问题,比如...

2023-06-04 22:44:18查看全文 >>

python打完代码后运行不提示(python代码写好了怎么运行不了)

python打完代码后运行不提示(python代码写好了怎么运行不了)

输出用print()在括号中加上字符串,就可以向屏幕上输出指定的文字。比如输出'hello, world',用代码实现如...

2023-06-04 22:53:26查看全文 >>

文档排行