python学习内容
循环
-
while
-
while 条件: 循环体
-
-
循环终止
-
条件改变
-
flag = True while flag: print(1) print(2) flag = False print(3) print(4)
-
-
break
-
flag = True while flag: print(1) print(2) # flag = False print(3) print(4) break
-
-
-
打印1-100所有的数
-
i = 1 s = 0 while i <= 100: s += i i += 1 print('1+2+...+100 = %d' % s)
-
-
打印1-100所有偶数
-
i = 1 s = 0 while i <= 100: if i % 2 == 0: s += i i += 1 print('2+4+...+100 = %d' % s)
-
-
运用
-
重复之前的动作时,比如在重复输入正确密码时可以运用
-
count = 1 while count <= 3: username = input('请输入你的名字:') password = input('请输入你的密码:') code = 'qqqq' your_code = input('请输入验证码:') if code == your_code: if username == 'xxx' and password == '1234': print('登陆成功') break else: print('用户名或者密码错误') else: print('用户验证码错误') count += 1
-
-
格式化输出
-
制作一个公共模板
-
%是可以作为一个占位符使用的,若要表示百分之几的意思,和c一样用%%表示
-
# 制作一个公共模板 name = input('请输入你的姓名:') age = input('请输入你的年龄:') job = input('请输入你的工作:') hobby = input('请输入你的爱好:') msg = \ ''' ---------- info of %s ---------- Name : %s Age : %s job : %s Hobbie : %s ---------- end ---------- ''' % (name, name, age, job, hobby) print(msg)
-
-
格式化输出
- 当遇到的需求:字符串中想让某些位置变成动态可传入时,需使用格式化输出
运算符优先级
- not > and > or