

Python入门系列(九)pip、try except、用户输入、字符串格式 - 生活处处有BUG
source link: https://www.cnblogs.com/bugs-in-life/p/16654985.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

包含模块所需的所有文件。
检查是否安装了PIP
$ pip --version
安装包
$ pip install package_name
import package_name
$ pip uninstall camelcase
pip list
Try Except
try:
print(x)
except:
print("An exception occurred")
您可以根据需要定义任意数量的异常块,例如,如果您想为特殊类型的错误执行特殊代码块
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
如果没有引发错误,可以使用else关键字定义要执行的代码块
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
如果指定了finally块,则无论try块是否引发错误,都将执行finally。
ry:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
这对于关闭对象和清理资源非常有用
try:
f = open("demofile.txt")
try:
f.write("Lorum Ipsum")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
except:
print("Something went wrong when opening the file")
作为Python开发人员,如果出现条件,您可以选择抛出异常。
x = -1
if x < 0:
raise Exception("Sorry, no numbers below zero")
您可以定义要引发的错误类型,以及要打印给用户的文本。
x = "hello"
if not type(x) is int:
raise TypeError("Only integers are allowed")
username = input("Enter username:")
print("Username is: " + username)
字符串格式
price = 49
txt = "The price is {} dollars"
print(txt.format(price)) # The price is 49 dollars
可以在花括号内添加参数,以指定如何转换值
price = 49
txt = "The price is {:.2f} dollars"
print(txt.format(price)) # The price is 49.00 dollars
如果要使用更多值,只需在format()方法中添加更多值
quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} dollars."
print(myorder.format(quantity, itemno, price))
您可以使用索引号(大括号{0}内的数字)确保将值放置在正确的占位符中
quantity = 3
itemno = 567
price = 49
myorder = "I want {0} pieces of item number {1} for {2:.2f} dollars."
print(myorder.format(quantity, itemno, price))
此外,如果要多次引用同一值,请使用索引号
age = 36
name = "John"
txt = "His name is {1}. {1} is {0} years old."
print(txt.format(age, name))
myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname = "Ford", model = "Mustang"))
您的关注,是我的无限动力!
公众号 @生活处处有BUG
Recommend
-
160
Aerotiger.info Aerotiger.info Copyright © 2022 | Privacy Policy
-
20
基础入门的知识一直没有更新完,今天小张接着给大家带来入门级的字符串的常用操作。本文适合刚入门的小白,大佬们请绕过。 一、定义 字符串的意思就是“一串字符”,比如“Hello,Charlie”是一个字符串,“How are you?”也是...
-
9
您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦。本文将重点介绍Python字符串的各种常用方法,字符串是实际开发中经常用到的,所有熟练的掌握它的各种用法显得尤为重要。 干货满满,建议收...
-
6
python 在finally中放return导致except中的raise没有抛出的坑 def e_try(): try: raise Exception('我的错') except Exception as e: print('捉到:%s' % e) raise finally:...
-
1
Python try except Translated to Golang Synonyms May 30, 2016 Synonyms -
-
2
逆向调试入门-PE结构-输入表输出表05/07 原创 最爱大苹果 2022-07-01 09:29:3...
-
3
-
8
Python进阶(25)—使用try-except语句捕获和处理异常◎知识点使用try-except语句捕获和处理异常◎脚本练习""" 程序在运行期间...
-
7
Python进阶(26)—try-except语句添加else和finally从句及手动抛出异常◎知识点在try-except语句的后面添加else从句在try-except语句的后面添加finally从句
-
6
来让 Bash 脚本有参数吧 😉你可以通过向 Bash 脚本传递变量来使其更加有用和更具交互性。让我通...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK