python学习笔记-day02 格式化输出

格式化输出

%s %d   其实%的作用就是占位

另外一种也可以使用forma 函数;

name=input("please input your name:")
age=input("please input your age:")
print("我的名字是{},我的年龄是{}".format(name,age))
print("我的名字是%s,我的年龄是%s" %(name,age))
View Code

如果想按照一定的格式输出,可以使用% 占位符格式化输出:

name='xuanxuan'
msg='我的名字是 %s' % name
print(msg)
name=input("please input your name:")
age=input("please input your age:")
height=input("please inpur your height:")
hobbi=input("please input your hobbi:")

msg='''--------------information of %s----------------
      name: %s
      age:  %d
      height: %d
      hobbi: %s
      ----------------------end-----------------------
      ''' %(name,name,int(age),int(height),hobbi)
      
print(msg)
      
View Code

如果你想在格式化输出的地方输出一个百分号比如:

name='xuanxuan'
age='12'
height=163
msg='name:%s,age:%s,height:%d,我是一个百分号5%' %(name,age,height)
print(msg)
View Code

其实是会报错的:

解决办法:

在想要输出%的前面再加一个%才能输出想要的%:

name='xuanxuan'
age='12'
height=163
msg='name:%s,age:%s,height:%d,我就是一个百分号5%%' %(name,age,height)
print(msg)
View Code

while... else

当while循环被break打断时,不会执行后面的else部分;当while循环正常执行完之后才会执行else部分;

这是while循环中当count累加到某个值,会跳出循环体,不会执行else

count=1
while True:
    print(count)
    count+=1
    if count>10:
        break
else:
    print("我不会被执行的,因为while循环体中当count累加到10之后就会跳出循环体")
View Code

下面是while循环体正常运行,else部分会被执行的情况:

count=1
while count<10:
    print(count)
    count+=1
else:
    print("while循环正常结束了,我是会被执行的哦~")
View Code

猜你喜欢

转载自www.cnblogs.com/xuanxuanlove/p/9457746.html