18.07.03

1.  循环  while循环

while 条件:

    代码块  #循环体
    运行流程: 如果条件为真,那么循环体则执行
             如果条件为假,那么循环体不执行
例 
count=1
while count<=5:
print("你很帅!")
count=count+1

如果在循环的过程中,因为某些原因,你不想继续循环了,怎么把它中止掉呢?这就用到break 或 continue 语句

break用于完全结束一个循环,跳出循环体执行循环后面的语句continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环

while True:
    s=input("请输入你的聊天内容:")
    if s=='q':
        break
    print("请开始你的表演:"+s)

while True:
s=input("请输入聊天内容:")
if s=="q":
break
if "马化腾"in s:
print("你输入的内容不合法!")
continue
print("请开始你的表演:"+s)

例  1+2+3+4+...+100=?

count=1
sum=0
while count<=100:
sum=sum+count
count=count+1
print(sum)

例  1--100之间的奇数

count=1
while count<=100:
if count%2!=0:
print(count)
count=count+1

while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

2.  格式化输出

%s就是代表字符串占位符,除此之外,还有%d,是数字占位符

name = input("请输入你的名字:")
age = input("请输入你的年龄:")
hobby = input("请输入你的爱好:")
gender = input("请输入你的性别:")
print("%s今年%s岁,爱好%s,性别%s" % (name,age,hobby,gender))

如果字符串中有占位符,那么后面所有的%都是占位,需要转译,如果没有占位符%可以直接用.

s = "网球"
print("%s受到全世界%%30人们的喜欢." % s)

3. 运算符

算数运算符:+,-,*,/,**,%,//

比较运算符:==,!=,<>(不等于),<,>,<=,>=

赋值运算:=,+=(例 a+=1相当于a=a+1),-=,*=,/=,**=,%=,//=

逻辑运算:

and (并且)左右两端都是真,运算结果为真

or(或者)左右两端有一端为真时.运算结果为真

not(非)原来是假,现在是真.

在没有()的情况下not 优先级高于 and,and优先级高于or,即优先级关系为( )>not>and>or,同一优先级从左往右计算

not 2>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6  #false
F and T or F and T and T or F
F or F or F
F

猜你喜欢

转载自www.cnblogs.com/gxj742/p/9260854.html