**
练习5 源代码
**
1 my_name = 'Zed A. Shaw' #姓名变量
2 my_age = 35 # not a lie #年龄变量
3 my_height = 74 # inches #身高变量
4 my_weight = 180 # lbs #体重变量
5 my_eyes = 'Blue' #眼睛颜色变量
6 my_teeth = 'White' #牙齿颜色变量
7 my_hair = 'Brown' #头发颜色变量
8
#打印字符串,及f对应的格式化{}中的变量,也将其转化为字符串。
9 print(f"Let's talk about {my_name}.")
10 print(f"He's {my_height} inches tall.")
11 print(f"He's {my_weight} pounds heavy.")
12 print("Actually that's not too heavy.")
13 print(f"He's got {my_eyes} eyes and {my_hair} hair.")
14 print(f"His teeth are usually {my_teeth} depending on the coffee.")
15
16 # this line is tricky, try to get it exactly right
17 total = my_age + my_height + my_weight #数字变量相加
18 print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.") #同上打印内容
知识点:
- 字母 f (代表 format),比如 f"Hello, {somevar}"。双引号前面的 f 是为了告诉 python3: “这个字符串需要被格式化,把这些变量放在那儿。” {}里为变量。
- print—— 打印内容
输出结果
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
附加练习
- 修改所有的变量,把前面的 my_ 删掉。要更改所有的变量名,而不只是有 = 的部分。
代码:
name = 'Zed A. Shaw'
age = 35 # not 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 {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 = age + height + weight
print(f"If I add {age}, {height}, and {weight} I get {total}.")
输出
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
- 试着写一些变量,把英尺(inches)和英镑(pounds)换算成厘米( centimeters)和千克(kilograms),别自己直接把自己的数据进去,用 python 的数学运算来换算。
代码
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_height_cent = my_height * 30.48 #centimeters
my_weight =180 # lbs
my_weight_kilo = my_weight * 0.454 #kilograms
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print(f"Let's talk about {my_name}.")
print(f"He's {my_height} inches or {my_height_cent} centimeters tall.")
print(f"He's {my_weight} pounds or {my_weight_kilo} 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}.")
输出
Let's talk about Zed A. Shaw.
He's 74 inches or 2255.52 centimeters tall.
He's 180 pounds or 81.72 kilograms heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
常见问题
- 变量名需要以字母开头,比如 a1。
- 可以用 round() 函数,比如:round(1.7333)。
print(round(180 * 0.454))
输出
82