2018/09/04循环+运算


一:while循环

定义: 只要条件成立,就一直循环执行循环代码

机构:

while 条件:

  循环执行代码

else:

  执行代码2

例如:

while True:

  print('0-0')

该函数则无限循环打印'0-0',直至天荒地老!

指令:break(推出本层循环)

       continue(推出本次循环)

 

 

二格式化输出

占位符:%  ( %d,占位指定取数值,%s: 占位指定取字符串,%r :  占位还原取值函数)

三 运算

python里面包含数学运算(如= - * / ),逻辑运算(and,or,not) 比较运算( ==   ,  <=  ,<= 等)

        and 并且。 左右两端同时为真。 结果才能是真
        or  或者。 左右两端有一个是真。 结果就是真
        not 非真即假, 非假即真

x or y  -> ( 如果x为0,取y,否则取x)

x and y   (如果x为0,则取X,否则取y)

运算顺序:  () > not > and > or

编码:

最早的计算机编码:ASCII 占8位

中国:
GBK : 占16位

最早的万国码:  unicode  占 32位

UTF-8: 可变长万国码,占位最短8位

UTF-16: 可变长万国码,占位最短16位

UTF-32: 可变长万国码,占位最短32位

 

 

print(1>1 or 3<4 or 4>5 and 2>1 and 9>8 or 7<6)  #True 
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 ) #false
print(8 or 3 and 4 or 2 and 0 or 9 and 7 ) #8
print(0 or 2 and 3 and 4 or 6 and 0 or 3 ) #4
print(6 or 2 > 1) #6
print(3 or 2 > 1) #3
print(0 or 5 < 4 ) # False
print(5<4 or 3) #3
print(2>1 or 6) # true
print(3 and 2>1) # 3
print(0 and 3>1) # 0
print(2>1 and 3) # 3
print(3>1 and 0) # 0
print(3>1 and 2 or 2<3 and 3 and 4 or 3>2) #2
while 条件:
   执行代码1
else:
   执行代码2

guess_num = int(input('请猜测幸运数字:'))
while guess_num != 66:
if guess_num > 66:
print('猜大了!')
    guess_num = int(input('请猜测幸运数字:'))
  elif guess_num < 66:
  print('猜小了!')
    guess_num = int(input('请猜测幸运数字:'))
else:
print('You are right !')
guess_num = int(input('请输入正确的数字:'))
count = 1
while guess_num != 66:
  if count ==3:
  print('Your are stupid !')
  break
elif guess_num > 66:
 print('猜大了!')
  elif guess_num <66:
  print('猜小了!')
elif guess_num == 66:
  break
    print('Your are right!')
    count += 1
  guess_num = int(input('请输入正确的数字:'))
else:
print('You are right !')
num = 1
while num <= 10:
  if num != 7:
  print(num)
 num = num + 1
count = 1
while count <10:
count += 1
 if count == 7:
continue
print(count)

sum = 0 count = 1
while count <= 100:
sum += count
count +=1
print(sum)
count = 1
while count <=50:
print(count * 2 -1)
count +=1
count = 1
while count <= 50:
print(count * 2)
 count += 1
count = 1 sum = 0
while count < 50:
a = count*2-1
b = a -count*2
sum = count * b + 99
count += 1 print(sum)
password = input('请输入您的密码:')
count = 1
while count < 3:
times = 3 - count
print("密码错误,剩余 %d 次机会" % (times))
password = input('请输入您的密码:')
count = count + 1
content = input('请输入广告词儿:')
while "" in content or "" in content or "稀缺" in content or "国家级" in content in content:
print("您的广告词非法!")
content = input('请输入广告词儿:')
else:
print('合法')

 

posted @ 2018-09-04 20:42  python传言  阅读(260)  评论(0编辑  收藏  举报