首页 > 技术文章 > 习题5 更多的变量和打印

tel13526327247 2018-06-29 10:05 原文

1.新建一个文件,文件的名称为ex5.py

 

2.将下面的代码复制到文件中

 

my_name = 'Zed A.shaw' #我的姓名 泽德A.肖
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}.")  #如果我们相加  , , 等于  。

 

3.找到ex4.py的文件,在该文件夹下空白处按住 Shift+鼠标右键,找到在此处打开Powershell窗口

 

4.在窗口内输入 python .\ex1.py 显示结果如下

 

 

5.巩固练习

 

  1.修改所有变量的名字,把它们前面的my_去掉,确定将每一个地方都改掉,不只是使用=设置的地方。

  2.试着使用变量将英寸和磅转换成厘米和千克。不要直接键入答案,使用Python的数学计算功能来完成。

 

6.常见问题解答

 

  1.这样定义变量行不行:1 = ‘Zed Shaw’?

    不行。1不是一个有效的变量名称。变量名要以字母开头,所以a1可以,但1不行。

  2.如何将浮点数四舍五入

    你可以使用round()函数,如round(1,7333)

推荐阅读