今天介绍了三种语句
if while循环 of 循环
一、if语句
1.if的概念
判断事物的对错,真假,是否可行。
2.为什么要有if判断
因为想要让计算机像人一样有判断事物对错的能力,从而做出不同的反应。
3.if条件:
gender = 'female'age = 19is_beautiful = Trueif gender == 'female'and age > 18 and age < 24 and is_beautiful: print('开始表白')print('end...')
4.if else 语式
gender = 'female'age = 17stature = 'slim'if gender == 'female'and age>17and age<24 and stature: # 判断是否符合条件 print('你好美女')else: # 不符合的话执行一下操作 print('再见兄弟')
user_from_db = "panyi"pwd_from_db = "123456"user_from_inp = input('please input your username<<<:')pwd_from_inp = input('please input your password<<<:')if user_from_db == user_from_inp and pwd_from_db ==pwd_from_inp: print('欢迎老板,24号技师为你服务')else : print('没钱滚蛋')
5.if…elif…else 语式
gender = 'female'age = 99stature = 'slim'if gender == 'female'and age>18 and age<24: print('你好,小姐姐')elif gender == "female"and age>27and age <35: print('你好,老姐姐')elif gender == "female"and age>18and age<26and stature=='slim': print("你好,小仙女")else: print('江湖再见')
6.练习
score = input("input your score<<<:")score = int(score)#需要定义一下 变量名不能和数字比较if score >= 90 and score <= 100: print('优秀')elif score >= 80: print('良好')elif score >= 70: print('普通')else: print("差")
today = input ('please input today>>>:')if today in [ 'Monday','Tuesday','Wednesday','Thursday','friday']:#可以用列表表示。 print('上班')elif today in ['saturday','sunday']: print('出去浪')else: print("""请输入以下时间( Monday Tuesday Wednesday Thursday friday saturday sunday) """)
二、while循环
from_db_username = "panyi"from_db_password = '12234'while True:#死循环 username = input('please input your username<<<:') password = input('please input your password<<<:') if from_db_password == password and from_db_username == username: print('登陆成功') else: print('登陆失败')
1.循环打印
break:立即结束本层循环(只针对它所属于的那一个while有效)
# 循环打印1-10n = 1while n < 11: print(n) n+=1 #循环打印1,2,3,4,,6,7,8,9,10 n = 1while n < 11: if n == 5: n = n + 1 continue# 跳过本次循环,直接开始下一次循环。不然会进入死循环。 print(n) n += 1
三、for循环
# name_list = ['nick', 'jason', 'tank', 'sean']# for name in name_list:# if name == 'jason':# break# print(name)# else:# print('for循环正常结束了')