• while循环


    流程控制之while循环

    1.什么是循环

    循环就是重复做某事

    2.为什么要有循环

    为了让计算机能够具备重复做某事的能力

    3.如何用循环

    while语法:
    while 条件:
    code1
    code2
    code3
    ....

    一:结束while循环的两种方式

    1.修改条件:等到下一次循环开始时判断条件为假才会结束while循环
    2.break:直接结束本层循环

    while True:
    while True:
    while True:
    break
    break
    break
    
    tag=True
    while tag:
    print(1)
    print(2)
    print(3)
    tag=False
    break
    print(4)
    案例一:
    name='tulipa'
    pwd='123'
    count=0
    while True:
    inp_name=input('please input your name: ')
    inp_pwd=input('please input your password: ')
    if inp_name == name and inp_pwd == pwd:
    print('login successful')
    break
    else:
    print('user or passwor error')
    count+=1 #count=3
    
    if count == 3:
    print('too many tries.....')
    break
    
    
    while count < 3:
    inp_name=input('please input your name: ')
    inp_pwd=input('please input your password: ')
    if inp_name == name and inp_pwd == pwd:
    print('login successful')
    break
    else:
    print('user or passwor error')
    count+=1 

    二:while+continue

    continue:结束本次循环,直接进入下一次循环
    不合理示范1:

    while True:
    print(1)
    print(2)
    print(3)
    # continue

    不合理示范2:

    while True:
    print(1)
    print(2)
    if 1 == 2:
    pass
    else:
    print('xxxx')
    continue

    案例一:

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

    三:while循环嵌套

    案例一:

    while True:
    inp_name=input('please input your name: ')
    inp_pwd=input('please input your password: ')
    if inp_name == name and inp_pwd == pwd:
    print('login successful')
    
    while True:
    print("""
    0 退出
    1 取款
    2 转账
    3 查询
    """)
    cmd = input('请输入指令编号>>>: ') # cmd='0'
    if cmd == '0':
    break
    elif cmd == '1':
    print('取款...')
    elif cmd == '2':
    print('转账...')
    elif cmd == '3':
    print('查询...')
    else:
    print("输入错误指令,请重新输入")
    break
    else:
    print('user or passwor error')
    count+=1 #count=3
    
    if count == 3:
    print('too many tries.....')
    break
    案例二:
    tag=True
    while tag:
    inp_name=input('please input your name: ')
    inp_pwd=input('please input your password: ')
    if inp_name == name and inp_pwd == pwd:
    print('login successful')
    
    while tag:
    print("""
    0 退出
    1 取款
    2 转账
    3 查询
    """)
    cmd = input('请输入指令编号>>>: ') # cmd='0'
    if cmd == '0':
    tag=False
    elif cmd == '1':
    print('取款...')
    elif cmd == '2':
    print('转账...')
    elif cmd == '3':
    print('查询...')
    else:
    print("输入错误指令,请重新输入")
    
    else:
    print('user or passwor error')
    count+=1 #count=3
    
    if count == 3:
    print('too many tries.....')
    break

    四:while+else

    else#如果while循环没有被break打断过,即正常运行完毕后才会执行else的子代码块
    n=0
    while True:
    # if n == 3:
    # break
    print(n)
    n+=1
    else:
    print('run.....')
    
    
    n=0
    while n <= 3:
    print(n)
    n+=1
    else:
    print('run.....')
    流程控制之for循环
    for循环再循环取值方面比while循环更为简洁
    s1=['a','b','c','d','e']
    i=0
    while i < len(s1):
    print(s1[i])
    i+=1
    for item in s1: #item='b'
    print(item)
    
    dic={'name':'egon','age':18,'sex':'male'}
    for k in dic:
    print(k,dic[k])
    for循环也一样
    1.for+break
    2.for+continue
    3.for+else 
    
    dic={'name':'egon','age':18,'sex':'male'}
    for k in dic:
    if k == 'age':break
    print(k,dic[k])
    else:
    print('run...')
    
    s1=['a','b','c','d','e']
    for i in range(len(s1)):
    print(i,s1[i])
    for i in range(1,5,2):
    print(i)

    可变与不可变类型

      可变类型:值改变,但id不变,证明是改变原值
      不可变类型:值改变,id也改变,证明是产生了新值,没有改变原值,例如int

    x=10
    print(id(x))
    x+=1
    print(id(x))
    
    l=['a','b','c']
    print(id(l))
    l[0]='A'
    print(id(l))

    练习
    1.如何查看变量在内存中的地址?

    x=1
    print(type(x)) #查看类型
    print(id(x)) #查看地址

    2.写代码
    实现用户输入用户名和密码,当用户名为 jason 且 密码为 123 时,显示登陆成功,否则登陆失败!

    ueser = 'jason'
    psd='123'
    in_user=input('请输入账号')
    in_pwd=input('请输入密码')
    if in_user==ueser and in_pwd==psd:
    print('登陆成功')
    else:
    print('账号或者密码错误')
    #实现用户输入用户名和密码,当用户名为 jason 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
    ueser = 'jason'
    psd='123'
    n=1
    while n<4:
    in_user=input('请输入账号')
    in_pwd=input('请输入密码')
    if in_user==ueser and in_pwd==psd:
    print('登陆成功')
    else:
    print('账号或者密码错误')
    n+=1
    print('三次机会结束')
    #实现用户输入用户名和密码,当用户名为 jason 或 tank 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
    n =1
    while n<4:
    in_ueser=input('请输入账号')
    in_psd=input('请输入密码')
    if in_ueser=='jason' and in_psd=='123':
    print('登陆成功')
    elif in_ueser=='tank' and in_psd=='123':
    print('登陆成功')
    else:
    print('账号或者密码错误')
    n+=1
    print('三次机会结束')

    3.求1-100的所有数的和

    n=0 #次数
    a=1
    while a<= 100:
    n+=a
    a+=1
    print(n)

    4.使用while循环实现输出2-3+4-5+6...+100 的和

    i = 2
    sum=0
    while i<=100:
    if i % 2 == 0:
    sum += i
    else:
    sum -= i
    i += 1
    print(sum)

    5.使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12

    n=1
    while n <= 12:
    if n != 6 and n!=10:
    print(n)
    n+=1

    6. 输出 1-100 内的所有奇数

    a=1
    while a<= 100:
    if a%2==1:
    print(a)
    a+=1

    7. 输出 1-100 内的所有偶数

    a=1
    while a<=100:
    if a%2==0:
    print(a)
    a+=1

    8.用户登陆(三次机会重试)实现用户输错用户名或者密码只有三次机会,超过三次则结束登陆。

    ueser = 'sjh'
    psd='123'
    n=1
    while n<4:
    in_user=input('请输入账号')
    in_pwd=input('请输入密码')
    if in_user==ueser and in_pwd==psd:
    print('登陆成功')
    else:
    print('账号或者密码错误')
    n+=1
    print('三次机会结束')

    9.猜年龄游戏
    要求:
    允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出

    num=48
    a=1
    while a<=3:
    user_num=input('请输入数字')
    user_num=int(user_num)
    if user_num>num:
    print('你输入大了')
    elif user_num<num:
    print('你输入小了')
    else:
    print('你猜对了')
    a+=1

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

    age=66
    n=0
    while True:
    if n == 3:
    choice=input('继续(Y/N?)>>: ')
    if choice == 'Y' or choice == 'y':
    n=0
    else:
    break
    
    guess=int(input('允许输入三次'))
    if guess == age:
    print('you got it')
    break
    n+=1

    11.编写计算器程序,要求
    1、用户输入quit则退出程序
    2、程序运行,让用户选择具体的计算操作是加法or乘法or除法。。。然后输入数字进行运算
    3、简单示范如下,可以在这基础上进行改进

    while True:
    msg='''
    1 加法
    2 减法
    3 乘法
    4 除法
    '''
    print(msg)
    choice = input('>>: ').strip()
    num1 = input('输入第一个数字:').strip()
    num2 = input('输入第二个数字:').strip()
    if choice == '1':
    res=int(num1)+int(num2)
    print('%s+%s=%s' %(num1,num2,res))
    
    
    while True:
    msg='''
    1 加法
    2 减法
    3 乘法
    4 除法
    '''
    print(msg)
    choice = input('>>: ').strip()
    num1 = input('输入第一个数字:').strip()
    num2 = input('输入第二个数字:').strip()
    if choice == '1':
    res=int(num1)+int(num2)
    print('%s+%s=%s' %(num1,num2,res))
    elif choice=='2':
    res=int(num1)-int(num2)
    print('%s-%s=%s' % (num1, num2, res))
    elif choice=='3':
    res=int(num1)*int(num2)
    print('%s*%s=%s' % (num1, num2, res))
    elif choice=='4':
    res=int(num1)/int(num2)
    print('%s/%s=%s' % (num1, num2, res))

    12.如何使用一行代码将列表info=[‘egon’,18, ’male’]中用户的名字,年龄,性别分别赋值给变量name, age, gender?

    info=['egon','18','male']
    name,age,gender=info
    print(name)
    print(age)
    print(gender)
  • 相关阅读:
    poj 2485 (kruskal)
    poj 1258
    poj 2253 (dijkstra 变形)
    poj 2485 (prim)
    poj 1125 (floyd)
    poj 2240 Arbitrage (floyd 变形)
    poj 3020 Antenna Placement(二分图+最小路径覆盖)
    poj 3020 Antenna Placement(二分图+最小路径覆盖)
    poj 3278 Catch That Cow (bfs 搜索)
    poj 2049 Finding Nemo(搜索)
  • 原文地址:https://www.cnblogs.com/ShenJunHui6/p/10208945.html
Copyright © 2020-2023  润新知