zoukankan      html  css  js  c++  java
  • while循环语句 格式化输出等一些练习

    while循环

    格式:

    while 关键字 空格 条件 冒号

    缩进 循环体

    break 终止
    continue 跳出本次循环,继续下次循环
    条件 可以控制while循环

    格式化输出

    msg = '你好%s,我是%s'%('乔狗','你大哥')
        print(msg)

    %s  %d== %i 占位   d和i必须放入的是整型  %s是不是放任何东西

    数量要对应
    在格式化中使用%的时候需要转义 %%

    运算符

    比较运算符
    >  <  >=  <=  ==  !=
    赋值运算符
    +=  -=  *=  /=  //=  **=  %=
    成员运算符
    in  not  in
    逻辑运算符
    and  or  not
    算数运算符
    +  -  *  /  **  %  //

    初识编码

    ascii 美国 256 没有中文
    一个字节 8位
    gbk 中国
    中文 2字节 16位
    英文 1字节 8位
    unicode 万国码
    2个字节 16位
    4个字节 32位
    utf-8 可变编码
    英文 1字节 8位
    欧洲 2字节 16位
    亚洲 3字节 24位

    单位转化
    # bit 位
    # bytes 字节

    # 1B == 8bit
    # 1024B = 1kB
    # 1024kB = 1MB
    # 1024MB = 1GB
    # 1024GB = 1TB

    我们来做一些案例

    判断下列逻辑语句的True,False

    1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6                  True

    2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6                False

    求出下列逻辑语句的值

    1),8 or 3 and 4 or 2 and 0 or 9 and 7                          8

    2),0 or 2 and 3 and 4 or 6 and 0 or 3                          4

    求下列结果

    1)、6 or 2 > 1                                6  

    2)、3 or 2 > 1                                3

    3)、0 or 5 < 4                                False

    4)、5 < 4 or 3                                3

    5)、2 > 1 or 6                                True

    6)、3 and 2 > 1                                True

    7)、0 and 3 > 1                                0

    8)、2 > 1 and 3                                3

    9)、3 > 1 and 0                                0

    10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2                    2

    利用while语句写出猜大小的游戏

    设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。

    num=66
    while 1:
        num1 = int(input("请输入你的猜想:"))
        if num1>num:
            print("猜大了")
        elif num1<num:
            print("猜小了")
        else:
            print("猜对了")
            break

    对以上的题进行升级

    给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示太笨了你....’

    num=66
    count=1
    while count<=3:
        num1 = int(input("请输入你的猜想:"))
        if num1>num:
            print("猜大了")
            count+=1
        elif num1<num:
            print("猜小了")
            count+=1
        else:
            print("猜对了")
            break
        if count>3:
            print("太笨了你")

    使用while循环输出 1 2 3 4 5 6 8 9 10

    sum=1
    while sum<11:
        if sum!=7:
            print(sum)
        sum+=1

    求1-100的所有数的和

    sum=0
    count=1
    while count<101:
        sum+=count
        count+=1
    print(sum)

    输出 1-100 内的所有奇数

    count=1
    while count<101:
        print(count)
        count+=2

    输出 1-100 内的所有偶数

    count=0
    while count<100:
        count+=2
        print(count)

    求1-2+3-4+5 ... 99的所有数的和

    第一种写法:

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

    第二种写法:

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

    第三种写法:

    1 i=0                #作为循环的的值
    2 sum=0           #作为最后打印总值
    3 j=-1               #作为运算时的符号
    4 while i<99:
    5     i+=1           #i自加1
    6     j=-j             #此处把符号变换     
    7     sum+=i*j     #sum等于i的值乘以符号,达到变号
    8 print(sum)         #打印sum                     

    ⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数

    uname="乔狗"
    upassword="666"
    count=3
    while count>0:
        username=input("输入你的用户名:")
        userpassword=input("输入密码:")
        if username==uname and userpassword==upassword:
            print("登录成功")
            break
        else:
            print("用户名或密码错误,你还有%s次机会" % (count-1))
            count-=1

     

  • 相关阅读:
    HiveQL:数据定义
    linux学习整理
    Hive中问题解决整理
    Hive 数据类型和文件格式
    neo4j 简要安装步骤
    Leetcode 刷题笔记二 两数相加(使用链表) -- scala版本
    leetcode 刷题笔记一 两数之和 scala版本
    windows 安装 tensorflow
    elemnetui 下载,及引用
    cenos7 安装mysql
  • 原文地址:https://www.cnblogs.com/53Dawns/p/10192914.html
Copyright © 2011-2022 走看看