- 1、本文档被系统程序自动判定探测到侵权嫌疑,本站暂时做下架处理。
- 2、如果您确认为侵权,可联系本站左侧在线QQ客服请求删除。我们会保证在24小时内做出处理,应急电话:400-050-0827。
- 3、此文档由网友上传,因疑似侵权的原因,本站不提供该文档下载,只提供部分内容试读。如果您是出版社/作者,看到后可认领文档,您也可以联系本站进行批量认领。
练习答案
这里提供了部分练习的答案。请不要着急看答案,设法搞明白新学的概念再看。另外,很多练习的
解决方案不止一种,这里列出的只是其中之一。
如果你在完成练习的过程中遇到困难,可前往StackOverflow、r/learnpython或在本书读者群中提问,
也可与作者联系。
2
第章
练习2.1简单消息
将一条消息赋给变量,并将其打印出来。
simple_message.py
msg=IlovelearningtousePython.
print(msg)
输出:
IlovelearningtousePython.
练习2.2多条简单消息
将一条消息赋给变量,并将其打印出来;再将变量的值修改为一条新消息,并将其打印出
来。
simple_messages.py
msg=IlovelearningtousePython.
print(msg)
msg=Itsreallysatisfying!
print(msg)
输出:
IlovelearningtousePython.
Itsreallysatisfying!
·1·
Python编程:从入门到实践(第3版)
练习2.3个性化消息
用变量表示一个人的名字,并向其显示一条消息。显示的消息应非常简单,如下所示。
HelloEric,wouldyouliketolearnsomePythontoday?
personal_message.py
name=eric
msg=fHello{name.title()},wouldyouliketolearnsomePythontoday?
print(msg)
输出:
HelloEric,wouldyouliketolearnsomePythontoday?
练习2.4调整名字的大小写
用变量表示一个人的名字,再分别以全小写、全大写和首字母大写的方式显示这个人名。
name_cases.py
name=eric
print(name.lower())
print(name.upper())
print(name.title())
输出:
eric
ERIC
Eric
练习2.5名言1
找到你钦佩的名人说的一句名言,将这个名人的姓名和名言打印出来。输出应类似于下面
这样(包括引号)。
AlbertEinsteinoncesaid,“Apersonwhonevermadeamistakenevertriedanythingnew.”
famous_quote.py
print(AlbertEinsteinoncesaid,Apersonwhonevermadeamistake)
print(nevertriedanythingnew.)
·2·
练习答案
输出:
AlbertEinsteinoncesaid,Apersonwhonevermadeamistake
nevertriedanythingnew.
练习2.6名言2
重复练习2.5,但用变量famous_person表示名人的姓名,再创建要显示的消息并将其赋
给变量message,然后打印这条消息。
famous_quote_2.py
famous_person=AlbertEinstein
message=f{famous_person}oncesaid,Apersonwhonevermadeamistake
nevertriedanythingnew.
print(message)
文档评论(0)