zoukankan      html  css  js  c++  java
  • 4 运算符 流程控制

    1 格式化输出

    现有一练习需求,问用户的姓名、年龄、工作、爱好 ,然后打印成以下格式

    name = input("Name:")
    age = int(input("Age:"))
    job = input("Job:")
    hometown = input("Hometown:")


    info = """
    -----------info of %s-----------
    Name: %s
    Age: %s
    Job: %s
    Hometown: %s
    -----------end -----------
    """ %(name,name,age,job,hometown)
    # s=string字符串
    # d= digit数字
    # f = float
    print(info)

    运行结果:

    1.1 占位符

    你怎么实现呢?你会发现,用字符拼接的方式还难实现这种格式的输出,所以一起来学一下新姿势

    只需要把要打印的格式先准备好, 由于里面的 一些信息是需要用户输入的,你没办法预设知道,因此可以先放置个占位符,再把字符串里的占位符与外部的变量做个映射关系就好啦

    name = input("Name:")
    age = input("Age:")
    job = input("Job:")
    hobbie = input("Hobbie:")
    
    info = '''
    ------------ info of %s ----------- #这里的每个%s就是一个占位符,本行的代表 后面拓号里的 name 
    Name  : %s  #代表 name 
    Age   : %s  #代表 age  
    job   : %s  #代表 job 
    Hobbie: %s  #代表 hobbie 
    ------------- end -----------------
    ''' %(name,name,age,job,hobbie)  # 这行的 % 号就是 把前面的字符串 与拓号 后面的 变量 关联起来 
    # %s string
    # %d int
    # %f float
    print(info)

    %s就是代表字符串占位符,除此之外,还有%d,是数字占位符,

    1.2 %d报错

    如果把上面的age后面的换成%d,就代表你必须只能输入数字啦

    age     : %d  

    我们运行一下,但是发现出错了。。。

    让我大声告诉你,input接收的所有输入默认都是字符串格式!

    要想程序不出错,那怎么办呢?简单,你可以把str转成int

    age = int(  input("Age:")  )
    print(type(age))

    肯定没问题了。相反,能不能把字符串转成数字呢?必然可以,str( yourStr )

    1.3 int 转换(%d)

    name = input("Name:")
    age = int(input("age:"))
    job = input("job:")
    hometown = input("hometown:")
    
    info = '''
    ----------info of %s-------
    Name:       %s
    age:        %d
    job:        %s
    hometown:   %s
    ---------end----------------
    '''%(name, name, age, job, hometown)
    print(info)

    运行结果:

    Name:alex
    age:22
    job:it
    hometown:xian
    
    ----------info of alex-------
    Name:       alex
    age:        22
    job:        it
    hometown:   xian
    ---------end----------------

    1.4 %f

    name = input("Name:")
    age = int(input("age:"))
    job = input("job:")
    hometown = input("hometown:")
    
    info = '''
    ----------info of %s-------
    Name:       %s
    age:        %f
    job:        %s
    hometown:   %s
    ---------end----------------
    '''%(name, name, age, job, hometown)
    print(info)
    
    # 执行结果
    Name:alex
    age:22
    job:it
    hometown:xian
    
    ----------info of alex-------
    Name:       alex
    age:        22.000000
    job:        it
    hometown:   xian
    ---------end----------------

    1.5 %s是万能的,一般用%s就OK

    name = input("Name:")
    age = int(input("age:"))
    job = input("job:")
    hometown = input("hometown:")
    print(type(name),type(age))
    
    info = '''
    ----------info of %s-------
    Name:       %s
    age:        %s
    job:        %s
    hometown:   %s
    ---------end----------------
    '''%(name, name, age, job, hometown)
    print(info)
    
    
    # 执行结果
    Name:alex
    age:22
    job:it
    hometown:xian
    <class 'str'> <class 'int'>
    
    ----------info of alex-------
    Name:       alex
    age:        22
    job:        it
    hometown:   xian
    ---------end----------------

    2 运算符

     2.1 算数运算符

    以下假设变量:a=10,b=20

    # 取模运算
    >>> 10%2
    >>> 10%3
    >>> 10%5
    
    # 判断奇偶数
    >>> 9%2
    >>> 10%2
    >>> 11%2
    >>> 12%2
    # 除法
    >>> 10/2
    5.0
    >>> 10/3
    3.3333333333333335
    
    
    # 取整除
    >>> 10//2
    >>> 10//3

    2.2 比较运算符

    以下假设变量:a=10,b=20

    >>> a
    >>> b
    >>> a == b
    False
    >>> a != b
    True
    >>> a <> b     # python3 没有这个方法了
      File "<stdin>", line 1
        a <> b
           ^
    SyntaxError: invalid syntax

    2.3 赋值运算符

    以下假设变量:a=10,b=20

    >>> a,b,c
    (15, 10, 3)
    >>>
    >>> c + a
    >>> c = c+a
    >>> c
    >>> c += a
    >>> c
    >>> c -= a
    >>> c
    >>> c = c - a
    >>>

    2.4 逻辑运算符

    以下假设变量:a=10,b=20

     3 流程控制

    3.1 单分支

    if 条件:
        满足条件后要执行的代码

    3.2 多分支

    if 条件:
        满足条件执行代码
    else:
        if条件不满足就走这段

    name = input("name:")
    gender = input("gender:")
    age = int(input("age:"))
    
    if gender == "" and age < 28 :
        print("i like the girl")
    else:
        print("姐弟恋也很好")
    
    # 运行结果1
    name:alex
    gender:男
    age:22
    姐弟恋也很好
    
    # 运行结果2
    name:syhanshan
    gender:女
    age:22
    i like the girl

         

    # 运行结果
    name:a;ex
    gender:男
    age:43
    let us go cs
    ---haha----
    
    # 运行结果
    name:alex
    gender:女
    age:29
    姐弟恋也很好
    ---haha----

    3.3 超多分支

    回到流程控制上来,if...else ...可以有多个分支条件

    if 条件:
        满足条件执行代码
    elif 条件:
        上面的条件不满足就走这个
    elif 条件:
        上面的条件不满足就走这个
    elif 条件:
        上面的条件不满足就走这个    
    else:
        上面所有的条件不满足就走这段
    • 猜年龄的游戏(版本1)
    age = 22
    n=0
    while n < 3:
        print("input you age")
        user_age = int(input())
        if user_age < age:
            print("small")
        elif user_age > age:
            print("big")
        else:
            print("bingo")
            break
        n += 1
    
        if n==3:
            choice = input("请问你还想继续游戏吗?y|n")
            if choice=='y':
                n=0
            else:
                break

    运行结果:

    C:UsersAdministratorPycharmProjectsuntitled1venvScriptspython.exe D:/DPJ-1/Demo/Demo/猜谜_1.py
    input you age
    10
    small
    input you age
    30
    big
    input you age
    22
    bingo
    
    Process finished with exit code 0

    版本二

    from random import randint
    num = randint(1,100)
    
    print("请输入你的数字")
    
    bingo = False
    
    while bingo == False:
        user_num = int(input())
    
        if user_num < num:
            print("small")
        elif user_num > num:
            print("big")
        else:
            print("bingo")
            bingo = True

    运行结果:

    C:UsersAdministratorPycharmProjectsuntitled1venvScriptspython.exe D:/DPJ-1/Demo/Demo/猜谜_2.py
    请输入你的数字
    10
    small
    50
    big
    40
    big
    30
    big
    20
    big
    15
    big
    11
    small
    14
    big
    13
    bingo
    
    Process finished with exit code 0
  • 相关阅读:
    对数据库中表的指定行的操作
    查找表中指定行的数据
    Smart.coder每日站立会议08
    SmartCoder每日站立会议07
    SmartCoder每日站立会议06
    SmartCoder每日站立会议05
    SmartCoder每日站立会议04
    SmartCoder每日站立会议03
    SmartCoder每日站立会议02
    SmartCoder每日站立会议 01
  • 原文地址:https://www.cnblogs.com/Mobai-c/p/10008056.html
Copyright © 2011-2022 走看看