美文网首页
笨办法学python3_ex.5

笨办法学python3_ex.5

作者: leogoforit | 来源:发表于2020-03-07 10:19 被阅读0次

第5个练习是变量和数学公式的综合练习,这里的用法就体现出python2与python3中的语法差异。编辑器输入如下代码:

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 heavey.")
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}.")

运行结果如下:


运行结果

其中 f 代替的是原来的 %str.format() 函数。
如果print("Let's talk about {my_name}.") 此句中不加f,则运行结果为:

Let's talk about {my_name}.

相关文章

网友评论

      本文标题:笨办法学python3_ex.5

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