python第四天

一、流程控制

  if判断

age_girl=31
if age_girl > 30:
print('阿姨好')
输出:阿姨好

age_girl=18
if age_girl > 30:
print('阿姨好')
else:
print('小姐好')
输出:小姐好

age_girl=18
height=171
weight=99
is_pretty=True
if age_girl >= 18 and age_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
print('表白')
else:
print('阿姨好')

if嵌套if
age_of_girl=18
height=171
weight=99
is_pretty=True
success=False
if age_of_girl >= 18 and age_of_girl < 21 and height > 170 and weight < 100 and is_pretty == True:
if success:
print('在一起')
else:
print('不相信爱情了')
else:
print('阿姨好')
案例:如果:成绩>=90,那么:优秀;如果成绩>=80且<90,那么:良好;如果成绩>=70且<80,那么:普通;其它情况:很差
grade=input('请输入你的成绩:')
#把输入的内容由字符串类型转换为整数类型
grade=int(grade)
if grade >= 90:
print('优秀')
elif grade >= 80:
print('良好')
elif grade >=70:
print('普通')
else:
print('很差')

#练习题,用户登录验证
name=input('请输入你的用户名:')
password=input('请输入的你密码:')
if name == 'yzn' and password == '1111':
print('login success')
else:
print('login fail')


#练习题,根据用户输入的内容输出其权限
'''
admin ---超级管理员
tom---普通管理员
yzn,jack---业务主管
'''
name = input('请输入用户的名字:')
if name == 'admin':
print('超级管理员')
elif name == 'tom':
print('普通管理员')
elif name == 'yzn' or name == 'jack':
print('业务主管')
else:
print('普通用户')
#练习题
'''
如果:今天是Monday,那么:上班
如果:今天是Tuesday,那么:上班
如果:今天是Wednesday,那么:上班
如果:今天是Thursday,那么:上班
如果:今天是Friday,那么:上班
如果:今天是Saturday,那么:出去浪
如果:今天是Sunday,那么:出去浪
'''
tody = input('今天星期几:')
if tody == 'Monday'or tody == 'Tuesday'or tody == 'Wednesday' or tody == 'Thursday' or tody == 'Friday':
print('上班')
elif tody == 'Saturday' or tody == 'Sunday':
print('出去浪')
else:
print('''
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
''')

  while条件循环

#打印0-10
count=0
while count <= 10:
print('loop',count)
count+=1
#练习,要求如下:
1 循环验证用户输入的用户名与密码
2 认证通过后,运行用户重复执行命令
3 当用户输入命令为quit时,则退出整个程序
'''
name='yzn'
passwd='123'
while True:
inp_name=input('用户名:')
inp_pwd=input('密码:')
if inp_name == name and inp_pwd == passwd:
while True:
cmd=input('>>:')
if not cmd:
continue
if cmd == 'quit':
break
print('run <%s>' %cmd)
else:
print('用户或密码错误')
continue
break

break与contiune
break:用于退出本层循环
while True:
print('123')
break
print('456')
输出:123
contiune用于退出本次循环,继续下一次循环
while True:
print('123')
continue
print('456')
输出:123

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

while循环练习题
#1. 使用while循环输出1 2 3 4 5 6     8 9 10
count=1
while count <= 10:
if count == 7:
count+=1
continue
print(count)
count+=1
#3. 输出 1-100 内的所有奇数
count = 1
while count <= 100:
if count%2 != 0:
print(count)
count += 1
#4. 输出 1-100 内的所有偶数
count = 1
while count <= 100:
if count%2 == 0:
print(count)
count+= 1
#6. 用户登陆(三次机会重试)
username='yzn'
password='1111'
count=0
while count <3:
in_username=input('输入用户名:')
in_password=input('输入密码:')
if in_username == username and in_password == password:
print('登录成功!')
break
count+=1
#7:猜年龄游戏
要求:
允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
my_age=18
count=0
while count < 3:
age=int(input('猜猜我的年龄:'))
if age == my_age:
print('恭喜猜对了')
break
count+=1
#8:猜年龄游戏升级版 
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
my_age=18
count=0
while True:
if count == 3:
choice=input('继续Y/N:')
if choice == 'Y' or choice == 'y':
count=0
else:
break
guess=int(input('>>:'))
if guess == my_age:
print('猜对了')
break
count+=1

  for循环

  迭代式循环:for

  for i in range(10):

    缩进的代码块

 

二、可变不可变类型

三、基本数据类型与内置方法:

  数字类型

  字符串

 

posted @ 2019-03-21 21:16  阳光与叶子  阅读(147)  评论(0编辑  收藏  举报