try expect
try 块允许您测试代码块以查找错误。
except 块允许您处理错误。
finally 块允许您执行代码,无论 try 和 except 块的结果如何。
多个异常 else finally
定义多个except
else:没有异常执行代码块
无论有没有except都会执行的代码块
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
else:
print("Nothing went wrong")
finally:
print("The 'try except' is finished")
引发异常
raise 抛出异常
x = -1
if x < 0:
raise Exception("Sorry, no numbers below zero")
命令行输入
input()
字符串格式化
多个值
只需要设置多个占位符{} ,占位符中可以加索引 {1},{2}, 也可以命名{name},{ss}
myorder = "I have a {carname}, it is a {model}."
print(myorder.format(carname = "Porsche", model = "911"))
标签:格式化,except,else,try,finally,基础教程,print
From: https://www.cnblogs.com/braveman1021/p/16998749.html