美文网首页Learn to Code
《笨办法学Python3》练习五:更多变量和练习

《笨办法学Python3》练习五:更多变量和练习

作者: 雨开Ame | 来源:发表于2019-02-27 18:40 被阅读0次

    练习代码

    my_name = 'Zed A. Shaw'
    my_age = 35 # not a lie
    my_height = 74 # inches
    my_weight = 180 # lbs
    my_eyes = 'Blue'
    my_teeth = 'White'
    my_hair = 'Brown'
    
    print(f"Let's talk about {my_name}.")
    print(f"He's {my_height} inches tall.")
    print(f"He's {my_weight} pounds heavy.")
    print("Actually that's not too heavy.")
    print(f"He's got {my_eyes} eyes and {my_hair} hair.")
    print(f"His teeth are usually {my_teeth} depending on the coffee.")
    
    # This line is tricky, try to get it exactly right
    total = my_age + my_height +  my_weight
    print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")
    

    Study Drills

    1. Change all the variables so there is no my_ in front of each one. Make sure you change the name everywhere, not just where you used = to set them.
    name = 'Zed A. Shaw'
    age = 35 # noyt a lie
    height = 74 # inches
    weight = 180 # lbs
    eyes = 'Blue'
    teeth = 'White'
    hair = 'Brown'
    
    print(f"Let's talk about {name}.")
    print(f"He's {height} inches tall.")
    print(f"He's {weight} pounds heavy.")
    print("Actually that's not too heavy.")
    print(f"He's got {my_eyes} eyes and {hair} hair.")
    print(f"His teeth are usually {teeth} depending on the coffee.")
    
    # this line is tricky, try to get it exactly right
    total = my_age + my_height +  my_weight
    print(f"If I add {age}, {height}, and {weight} I get {total}.")
    
    1. Try to write some variables that convert the inches and pounds to centimeters and kilograms.
      Do not just type in the measurements. Work out the math in Python.
    my_name = 'Zed A. Shaw'
    my_age = 35 # noyt a lie
    my_height = 74 # inches
    my_height =  my_height * 2.54 # centimeters
    my_weight = 180 # lbs
    my_weight = my_weight * 0.4535924 # kilograms
    my_eyes = 'Blue'
    my_teeth = 'White'
    my_hair = 'Brown'
    
    print(f"Let's talk about {my_name}.")
    print(f"He's {my_height} centimeters tall.")
    print(f"He's {my_weight} kilograms heavy.")
    print("Actually that's not too heavy.")
    print(f"He's got {my_eyes} eyes and {my_hair} hair.")
    print(f"His teeth are usually {my_teeth} depending on the coffee.")
    
    # this line is tricky, try to get it exactly right
    total = my_age + my_height +  my_weight
    print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")
    

    补充

    1. 使用round()函数精确浮点数

    官方文档描述

    举例:

    num = 1682.675
    
    print(round(num)) # 第二个参数省略时,返回整数
    print(round(num, 0)) # 精确到个位,1683.0
    print(round(num, 1)) # 十分位,1683.7
    print(round(num, 2)) # 百分位1683.67 因为计算机显示精度的原因,不是 1683.68
    print(round(num, -1)) # 十位
    print(round(num, -2)) # 百位
    
    print(round(-0.5))
    print(round(0.5)) # 特殊
    

    相关文章

      网友评论

        本文标题:《笨办法学Python3》练习五:更多变量和练习

        本文链接:https://www.haomeiwen.com/subject/klnfuqtx.html