Last updated 6 years ago
Was this helpful?
pass语句是空语句,它是为了保持程序结构的完整性,一般用做占位语句。
我们还是用刚刚这个例子:
在这个程序中,我们可以用pass来代替continue,然后在下面多加上一个else语句。在刚刚的程序中,“without9 += digit”这条语句和if语句是平行结构,而在现在这个程序中是if else语句,也就是两者选其一的结构:当这个数字为9就不加上,如果不是9就加上去。两个程序实现了一样的结果。
# 求在2的100次方中删除所有的9后的数字: num = 2**100 without9 = '' for digit in str(num): if digit == "9": pass else: without9 += digit print("2**100 is: %d \nwithout 9 is :%s" %(num,without9))