小白学python day4:流程控制

一、流程控制---if

  1.if条件判断

  

age=18
hight=1.70
sex="female"
is_beautiful=True
if sex=="female" and age>16and age<20 and is_beautiful \
        and hight>1.60 and hight<1.75:
    print("表白。。。。")

  2.if+else


hight=1.70 sex="female" is_beautiful=True if sex=="female" and age>16and age<20 and is_beautiful \ and hight>1.60 and hight<1.75: print("表白。。。。") if is_beautiful: print("在一起。。。") else: print("什么爱情不爱情""和我有半毛钱关系")

  3.if的嵌套:

#语法三:if 的嵌套
age=18
hight=1.70
sex="female"
is_beautiful=True
if sex=="female" and age>16and age<20 and is_beautiful \
        and hight>1.60 and hight<1.75:
    print("表白。。。。")
    if is_beautiful:
        print("在一起。。。")
    else:
        print("什么爱情不爱情""和我有半毛钱关系")
else:
    print("阿姨好。。。")
print(" code ...")

  4.if-elif的运用:

如果 成绩>=90,那么:优秀

如果 成绩>=80且<90,那么:良好

如果 成绩>=70且<80,那么:普通

其他情况:很差
"""score=input("输入成绩:")
score=int(scar)
if scar>=90:
    print("优秀")
elif score>=80:
    print("良好")
elif score>=70:
    print("普通")
else:
    print("极差")"""

 二、流程控制--while

  1.while循环又称为条件循环

  

   while条件循环
"""

while True:
    name=input("please input yourname:")

    password=input("please input your passwor;")

    if name=="adb" and password=="123":

        print("login successful")

    else:

         print("username or password error")

  2.while+条件结束

  

tag=True
while tag:
    name=input("please input yourname:")

    password=input("please input your passwor;")

    if name=="adb" and password=="123":

        print("login successful")

        tag=False

    else:

         print("username or password error")

  3.while+break:退出本层循环

  

while True:
    name = input("please input yourname:")

    password = input("please input your passwor;")

    if name == "adb" and password == "123":

        print("login successful")

        break

  4.while+continue:退出本次循环,继续下面循环

  

ount=0
while count<6:
    if count==4:
       count+=1
    continue
    print(count)
    count+=1

 5.while+else:当while循环正常执行完,中间没有break中止的话,执行else

  

# ---while-else----
count = 1
while count < 6:
    if count == 4:
        count += 1
        continue

    print(count)
    count += 1
else:
    print("while-else最后else的代码")

  三、流程控制--for

  for循环又称为迭代循环
  for可以不依赖于索引取指,是一种通用的循环取指方式
  循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的

  1.0 range()

  

range(1,5)  
>>>1 2 3 4#g顾头不顾尾,
所以只能取到

range(5)
>>>1 2 3 4

range(1,5,2)  #补偿 ,每隔两个取值#i = i + 2
>>>1 3

  

  2.0嵌套循环:

for i in range(1,10):
        for j in range(1,i+1):

  本章练习:

 .练习,要求如下:

  1.1用户登录:

  

name=input("please your name:")
password=input("please your password:")


if name =="egon" and password=="123456":
    print("login successful....")
    break
else:
    print("name or password error....")

  1.2管理员登录系统:

  

egon --> 超级管理员
tom  --> 普通管理员
jack,rain --> 业务主管
其他 --> 普通用户
ame=input("请输入名字:")
if name=="egon":
    print("超级管理员")
elif name=="tom":
    print("普管理员")
elif name=="jick,rain":
    print("业务主管")
else:
    print("普通用户")

  1.3 

  

 1 如果:今天是Monday,那么:上班
 2 # 如果:今天是Tuesday,那么:上班
 3 # 如果:今天是Wednesday,那么:上班
 4 # 如果:今天是Thursday,那么:上班
 5 # 如果:今天是Friday,那么:上班
 6 # 如果:今天是Saturday,那么:出去浪
 7 # 如果:今天是Sunday,那么:出去浪
 8 
 9 """
10 """
11 today_is=input("今天是礼拜几:")
12 if  today_is=="monday" or today_is=="tuestday" or today_is=="wedneesday"or \
13         today_is=="thursday"or today_is=="friday":
14     print("working...")
15 elif today_is=="sunday" or today_is=="saturday":
16     print("playing...")
17 else:
18     print("必须输入:"
19         "monday"
20     "Tuesday",
21     "Wednesday",
22     "Thursday",
23     "Friday",
24     "Saturday",
25     "Sunday"   )
View Code

  2.1.while循环练习题
        1 循环验证用户输入的用户名与密码
        2 认证通过后,运行用户重复执行命令 3 当用户输入命令为时,则退出整个程序 

  

name = "egon"
password = "123456"

count = 0
while True:

    inp_name = input("please your name:")
    inp_password = input("please your password:")
    if name == inp_name and password == inp_password:
        print("login successful....")
        print("""
        1 取钱
        2 存款
        3 退出
        
        """)
        while True:
            choice = input("please input your choice")
            if choice == "1":
                print("取钱")
            elif choice == "2":
                print("存钱")
            elif choice == "3":
                break
            else:
                print("你的输入有误,请重新输入")
    else:
        print("name or password error....")
        count += 1
        if count == 3:
            break

  2.1 迭代式循环:for for in range():作业九九乘法表

  

L = []
for i in range(1,10):
        for j in range(1,i+1):
                x=i*j

                m="%s*%s=%s"%(i,j,x)

                L.append(m)
        print(L)
        L = []

 2.2 制作金字塔

  pass

  

猜你喜欢

转载自www.cnblogs.com/mofujin/p/10574768.html