<More Variables and Printing>
my_name = 'zed a shaw' #将名字赋值给my name
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}")
运行结果:
/Users/tongshiba/PycharmProjects/ex3/test452.py
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
Process finished with exit code 0
练习题:
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.
done
2. 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.
done
练习题2解答过程
my_name = 'zed a shaw' #将名字赋值给my name
my_age = 35 #not a lie
my_height = 74 # inches
my_weight = 180 #lbs
my_eyes = 'blue'
my_teeth = 'white'
my_hair = 'brown'
my_weight_kg = 0.454 * my_weight
my_height_cm = 2.54 * my_height
print(f"let's talk about {my_name}.") #打印字符串,感变量加了{}变成了字符串
print(f"he's {my_height_cm} inches tall.")
print(f"he's {my_weight_kg} 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_cm}, and {my_weight_kg} i get {total}")
常见问题Common Student Questions
1.Can I make a variable like this: 1 = 'Zed Shaw'?
No, 1 is not a valid variable name. They need
to start with a character, so a1 would work, but 1 will not.
2.How can I round a floating point number?
You can use the round() function like this:
round(1.7333).
round(1.73) 四舍五入是2
3.Why does this not make sense to me?
Try making the numbers in this script your measurements.it’s weird, but talking about yourself will make it seem more real. Also, you’re just starting out so it won’t make too much sense. Keep going and more exercises will explain it more
网友评论