Python--习题 5: 更多的变量和打印(初学笔记)

学习环境:

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))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心旅行缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值