PYTHON-DAY4-流程控制

流程控制之if
流程控制之while/for
数据类型及内置方法part1:(int/float/str/list)

=================================================

# 流程控制之if判断
#
# 1 什么是if判断
# 判断一个条件如果成立则做...不成立则做....
# 2 为何要有if判断
# 让计算机能够像人一样具有判断的能力
#
# 3 如何用if判断

# 语法1:
'''
if 条件1:
code1
code2
code3
......
'''

# 语法2:
'''
if 条件:
code1
code2
code3
......
else:
code1
code2
code3
......
'''

# 语法3:
'''
if 条件1:
if 条件2:
code1
code2
code3
code2
code3
......
'''

# 语法4:
#
# if 条件1:
# 子代码块1
# elif 条件2:
# 子代码块2
# elif 条件3:
# 子代码块3
# elif 条件4:
# 子代码块4
# .........
# else:
# 子代码块5
#
# '''
# # 成绩评分
# # score1 = 90
# # score2 = 80
# # score = int(input('请输入成绩'))
# # if score > score1:
# # print('优秀')
# # elif score2 < score > score1:
# # print('良好')
# # else:
# # print('一般')
#
# # 如果:女人的年龄>30岁,那么:叫阿姨,否则:叫小姐
# # age = int(input('pls input ur age'))
# # if age > 30:
# # print('叫阿姨')
# # else:
# # print('叫小姐')
#
#
# # 如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨
# ##在表白的基础上继续:
# #如果表白成功,那么:在一起
# #否则:打印。。。
# # age = int(input('pls input ur age'))
# # weight = int(input('pls input ur wg'))
# # height = int(input('pls input ur hg'))
# # is_pretty = True
# # success = True
# # if 22>age>=18 and weight<100 and height>170 and is_pretty == True:
# # print('表白')
# # if success == True:
# # print('在一起')
# # else:
# # print('打印。。。')
# # else:
# # print('叫阿姨')
#
# # 根据用户输入内容输出其权限
# # egon --> 超级管理员
# # tom --> 普通管理员
# # jack,rain --> 业务主管
# # 其他 --> 普通用户
#
# # name = input('请输入名字')
# # if name == 'egon':
# # print('超级管理员')
# # elif name == "tom":
# # print("普通管理员")
# # elif name == "jack" or name == "rain":
# # print("普通管理员")
# # else:
# # print("普通用户")
#
# # 如果:今天是Monday,那么:上班
# # 如果:今天是Tuesday,那么:上班
# # 如果:今天是Wednesday,那么:上班
# # 如果:今天是Thursday,那么:上班
# # 如果:今天是Friday,那么:上班
# # 如果:今天是Saturday,那么:出去浪
# # 如果:今天是Sunday,那么:出去浪
#
# # today1 = input("今天是星期几")
# # if today1 == "Saturday" or today1 =="Sunday":
# # print("出去浪")
# # elif today1 == "Monday" or today1 == "Tuesday" or today1 == "Wednesday"or today1 == "Thursday"or today1 == "Friday":
# # print("上班")
#
# # today = input("今天是星期几")
# # if today in["Saturday" ,"Sunday"]:
# # print("出去浪")
# # elif today in["Monday" ,"Tuesday","Wednesday","Thursday","Friday"]:
# # print("上班")
# '''
#
# 流程控制之while循环
# 1. 什么是循环
# 循环指的是一个重复做某件事的过程
#
# 2. 为何要有循环
# 为了让计算机能够像人一样重复做某件事
# 3. 如何用循环

# while循环的语法:while循环又称为条件循环,循环的次数取决于条件
'''
while 条件:
子代码1
子代码2
子代码3
'''

# 如何结束while循环
# 方式一:操作while循环的条件让其结束
# 方式二: break强行终止本层循环

# while+continue:continue代表结束本次循环,直接进入下一次
# count=1
# while count < 6:
# if count == 4:
# count+=1
# continue # 只能在cotinue同一级别之前加代码
# print(count)
# count+=1
#
# while True:
# print('11111')
# print('22222')
# print('333')
# continue # 不应该将continue作为循环体最后一步执行的代码


# while+else
# count=1
# while count < 6:
# if count == 4:
# break
# print(count)
# count+=1
# else:
# print('会在while循环没有被break终止的情况下执行')

# while循环的嵌套
# tag控制所有while循环

'''
# print('start...')
# tag=True
# while tag:
# um=input('ur username>>>')
# pwd=input('ur password>>>')
# if um == 'abc' and pwd == '123':
# print('login in')
# tag=False
# else:
# print('username or password error')
# print('end...')

# print('start...')
# count=0
# while count <3:
# um=input('ur username>>>')
# pwd=input('ur password>>>')
# if um == 'abc' and pwd == '123':
# print('login in')
# break
# else:
# print('username or password error')
# count+=1
# if count == 3:
# print('3 error,sxx')
# break
# print('end...')


# #打印0-10
# count=0
# while count <=10:
# print(count)
# count+=1

# #打印0-10之间的偶数

# count = 0
# while count <= 10 :
# if count %2 ==0 :
# print(count)
# count += 1

# #打印0-10之间的奇数
#
# #练习,要求如下:
# 1 循环验证用户输入的用户名与密码
# 2 认证通过后,运行用户重复执行命令
# 3 当用户输入命令为quit时,则退出整个程序


# tag = True
# while tag:
# username = input('ur un')
# password = input('ur pw')
# if username == 'abc' and password == '123':
# print('login in')
# while tag:
# cmd=input('>>>')
# if cmd == 'quit':
# tag =False
# break
# elif not cmd:
# continue
# print('put...%s' % cmd)
# else :
# print('un or pw error,pls try again')


# #1. 使用while循环输出1 2 3 4 5 6 8 9 10
# count=1
# tag =True
# while tag:
# if count<=10:
# if count==7:
# count += 1
# print(count)
# count+=1

# count=1
# while count<=10:
# if not count==7:
# print(count)
# count+=1

# #2. 求1-100的所有数的和
# res= 0
# count=1
# while count<=100:
# res+=count
# count+=1
# print(res)


# #3. 输出 1-100 内的所有奇数
# count=1
# while count<=100:
# if count%2 ==1:
# print(count)
# count += 1

# #4. 输出 1-100 内的所有偶数
# #5. 求1-2+3-4+5 ... 99的所有数的和

# res= 0
# count=1
# while count<100:
# if count%2 ==0:
# res-=count
# else:
# res += count
# count+=1
# print(res)


# #6. 用户登陆(三次机会重试)
# tag= 0
# while tag<3:
# name=input('ur name')
# pw=input('ur pw')
# if name =='abc' and pw=='123':
# print('login in')
# tag = False
# break
# else:
# print('pls try again')
# tag+=1


# #7:猜年龄游戏
# 要求:
# 允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
# age_gg='18'
# count=0
# while count<3:
# age=input('age')
# if age != age_gg:
# print('error')
# count+=1
# else:
# print('congrutation_')
# count=3

# #8:猜年龄游戏升级版
# 要求:
# 允许用户最多尝试3次
# 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
# 如何猜对了,就直接退出

# age_gg='18'
# tag=True
# count=0
# while tag:
# age=input('age')
# if age == age_gg:
# print('congrutation_')
# tag=False
# else:
# print('error')
# count+=1
# if count == 3:
# chioce = input('go on or not,pls answer Y or N>>>')
# if chioce == 'y' or chioce == 'Y':
# count = 0
# elif chioce == 'N' or chioce == 'n':
# tag = False

'''

# for循环主要用于循环取值


# name=['abc','bcd','cde','def']
# i=0
# while i<len(name):
# print(name[i])
# i+=1

# for i in name:
# print(i)

# for item in 'hello world':
# print(item)

# dic={'aa':11,'bb':'22','cc':'33'}
# for k in dic:
# print(k,dic[k])

# for i in range(1,10,3):
# print(i)
# for i in range(10):
# print(i)

猜你喜欢

转载自www.cnblogs.com/du-jun/p/9640591.html