文件和异常练习2——PYTHON编程从入门到实践
Posted heimaguangzhou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件和异常练习2——PYTHON编程从入门到实践相关的知识,希望对你有一定的参考价值。
[url=][/url]
print("Enter two numbers, and I‘ll add them.")first_number = input("
First number: ")second_number = input("Second number: ")try: nums_sum = int(first_number) + int(second_number) print(first_number + ‘+‘ + second_number + ‘=‘ + str(nums_sum))except ValueError: print("Sorry! Only accept numbers input.")[url=][/url]
[url=][/url]
print("Enter two numbers, and I‘ll add them.")print("Enter ‘q‘ to quit.")while True: first_number = input("
First number: ") if first_number == ‘q‘: break second_number = input("Second number: ") if second_number == ‘q‘: break try: nums_sum = int(first_number) + int(second_number) print(first_number + ‘+‘ + second_number + ‘=‘ + str(nums_sum)) except ValueError: print("Sorry! Only accept numbers input.")[url=][/url]
[url=][/url]
file_names = [‘cats.txt‘, ‘dogs.txt‘]for filename in file_names: try: with open(filename) as f_obj: names = f_obj.read() print(filename + " include:
" + names.rstrip()) except FileNotFoundError: print("Sorry, the file " + filename + " can not fond.")[url=]
[/url]
[url=]
[/url]
file_names = [‘cats.txt‘, ‘dogs.txt‘]for filename in file_names: try: with open(filename) as f_obj: names = f_obj.read() print(filename + " include:
" + names.rstrip()) except FileNotFoundError: pass[url=]
[/url]
更多技术资讯可关注:gzitcast
以上是关于文件和异常练习2——PYTHON编程从入门到实践的主要内容,如果未能解决你的问题,请参考以下文章