学习环境:
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
原代码内容:
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 "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth
# this line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d." % (
my_age, my_height, my_weight, my_age + my_height + my_weight)
执行报错:
D:\Mystuff>python ex8-0.py
File "ex8-0.py", line 9
print "Let's talk about %s." % my_name
^
SyntaxError: Missing parentheses in call to 'print'
修改及结果如下:
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 ("Let's talk about %s.") % my_name #错误书写
#Let's talk about %s.
#Traceback (most recent call last):
# File "ex8.py", line 17, in <module>
# print ("Let's talk about %s.") % my_name
#TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
print ("Let's talk about %s." % my_name ) #正确书写
#Let's talk about Zed A. Shaw.
print ("He's %d inches tall." % my_height)
#He's 74 inches tall.
print ("He's %d pounds heavy." % my_weight)
#He's 180 pounds heavy.
print ("Actually that's not too heavy.")
#Actually that's not too heavy.
#print ("He's got %s eyes and %s hair.") % (my_eyes, my_hair) #错误书写
#报错同上18-22行
print ("He's got %s eyes and %s hair." % (my_eyes, my_hair)) #正确书写
#He's got Blue eyes and Brown hair.
print ("His teeth are usually %s depending on the coffee." % my_teeth)
#His teeth are usually White depending on the coffee.
#This line is tricky, try to get it exactly right
#print ("If I add %d, %d, and %d I get %d.") % (my_age, my_height, my_weight, my_age + my_height + my_weight) #错误书写
#报错同上18-22行
print ("If I add %d, %d, and %d I get %d." %
(my_age, my_height, my_weight, my_age + my_height + my_weight))
#If I add 35, 74, and 180 I get 289.
加分习题
#修改所有变量名称,把前面的“my_” 去掉
#使用变量将英寸和磅转换成厘米和千克,使用Python的计算功能来完成
name = 'Zed A. Shaw'
age = 35 # not a lie
height = 74 # inches
transfer_height = 2.54 * height # cm
weight = 180 # lbs
transfer_weight = 0.45359237 * weight # kg
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
print ("Let's talk about %s." % name )
print ("He's %d inches tall." % height)
print ("He's %d inches tall, %d cm." %(height, transfer_height))
#He's 74 inches tall, 187 cm.
print ("He's %d pounds heavy." % weight)
print ("He's %d pounds heavy, %d kg." %(weight, transfer_weight))
#He's 180 pounds heavy, 81 kg.
print ("Actually that's not too heavy.")
print ("He's got %s eyes and %s hair." % (eyes, hair))
print ("His teeth are usually %s depending on the coffee." % teeth)
#This line is tricky, try to get it exactly right
print ("If I add %d, %d, and %d I get %d." %
(age, height, weight, age + height + weight))