《Python 编程从⼊门到实践第⼆版》第⼗章练习
10-1 Python学习笔记 在⽂本编辑器中新建⼀个⽂件,写⼏句话来总结⼀下你⾄此学到的Python知识,其中每⼀⾏都以“In Python you can”打头。将这个⽂件命名为,并存储到为完成本章练习⽽编写的程序所在的⽬录中。编写⼀个程序,它读取这个⽂件,并将你所写的内容打印三次:第⼀次打印时读取整个⽂件;第⼆次打印时遍历⽂件对象;第三次打印时将各⾏存储在⼀个列表中,再在with代码块外打印它们。
10-2 C语⾔学习笔记 可使⽤⽅法replace() 将字符串中的特定单词都替换为另⼀个单词。下⾯是⼀个简单的⽰例,演⽰了如何将句⼦中的’dog’ 替换为’cat’ : message = “I really like dogs.” place(‘dog’, ‘cat’) ‘I really like cats.’ #读取你刚创建的⽂件中的每⼀⾏,将其中的Python都替换为另⼀门语⾔的名称,⽐如C。将修改后的各⾏都打印到屏幕上。10-3 访客 编写⼀个程序,提⽰⽤户输⼊名字。⽤户做出响应后,将其名字写⼊⽂件中。
10-4 访客名单 编写⼀个while 循环,提⽰⽤户输⼊名字。⽤户输⼊名字后,在屏幕上打印⼀句问候语,并将⼀条到访记录添加到⽂件中。确保这个⽂件中的每条记录都独占⼀⾏。
10-5 调查 编写⼀个while 循环,询问⽤户为何喜欢编程。每当⽤户输⼊⼀个原因后,都将其添加到⼀个存储所有原因的⽂件中。In Python you can write AI!In Python you can read program!
1
2# 第⼀次打印时读取整个⽂件with  open ('') as  f : print (f .read ())# 第⼆次打印时遍历⽂件对象with  open ('') as  f : for  line in  f :  print (line .rstrip ())# 第三次打印时将各⾏存储在⼀个列表中,再在with 代码块外打印它们with  open ('') as  f : lines = f .readlines ()for  line in  lines : print (line .rstrip ())f .close ()
1
2
3
4
5
6
7
8
9
1011
12
13
14
15
16with  open ('') as  f : contents = f .read () print (contents .replace ('Python','C'))f .close ()
1
2
3
4filename = ''name = input ('请输⼊您的名字:')with  open (filename ,'w',encoding ='utf-8') as  f : print (f .write (name ))f .close ()
1
2
3
4
5filename = ''while  True : name = input ('请输⼊您的名字:') msg = print (f'欢迎{name }访问\n') with  open (filename ,'w',encoding ='utf-8') as  f :  print (f .write (msg ))  f .close ()
html block1
2
3
4
5
6
7
10-6 加法运算 提⽰⽤户提供数值输⼊时,常出现的⼀个问题是,⽤户提供的是⽂本⽽不是数。在此情况下,当你尝试将输⼊转换为整数时,将引发ValueError 异常。编写⼀个程序,提⽰⽤户输⼊两个数,再将其相加并打印结果。在⽤户输⼊的任何⼀个值不是数时都捕获ValueError 异常,并打印⼀条友好的错误消息。对你编写的程序进⾏测试:先输⼊两个数,再输⼊⼀些⽂本⽽不是数。
10-7 加法计算器 将为完成练习10-6⽽编写的代码放在⼀个while 循环中,让⽤户犯错(输⼊的是⽂本⽽不是数)后能够继续输⼊数。
10-8 猫和狗 创建⽂件和,在第⼀个⽂件中⾄少存储三只猫的名字,在第⼆个⽂件中⾄少存储三条狗的名字。编写⼀个程序,尝试读取这些⽂件,并将其内容打印到屏幕上。将这些代码放在⼀个try-except代码块中,以便在⽂件不存在时捕获FileNotFound错误,并显⽰⼀条友好的消息。将任意⼀个⽂件移到另⼀个地⽅,并确认except 代码块中的代码将正确执⾏。
10-9 静默的猫和狗 修改你在练习10-8中编写的except代码块,让程序在任意⽂件不存在时静默失败。
Jenny A filename = ''while  True : problem = input ('请回答,您为何喜欢编程?') with  open (filename ,'w',encoding ='utf-8') as  f :  print (f .write (problem ))  f .close ()
1
2
34
5
6num1 = input ('请输⼊第⼀个数:')num2 = input ('请输⼊第⼆个数:')try : print (int (num1)+int (num2))except  ValueError : print ('您输⼊的不是数!')
1
2
3
4
5
6while  True : num1 = input ('请输⼊第⼀个数:') um2 = input ('请输⼊第⼆个数:') try :  print (int (num1)+int (num2))  break  except  ValueError :  print ('您输⼊的不是数!')
1
2
3
4
5
6
7
8filenames = ['','']for  i in  filenames : try :  with  open (i ,encoding ='utf-8') as  f :  contents = f .read ()  print (contents ) except  FileNotFoundError :  print ('⽂件不存在')
1
2
3
4
5
67
8
9filenames = ['','']for  i in  filenames : try :  with  open (i ,encoding ='utf-8') as  f :  contents = f .read ()  print (contents ) except  FileNotFoundError :  pass
1
2
3
4
5
6
7
8
9
js使用正则表达式
The July day was sinking into evening, an evening light that was soft and mellow in spite of the line of stormcloud
above the cathedral. It was the first bright day that had been known for many weeks, and all available hands had been turned to work upon the hay which, green and damp still from recent exper
iences, was lying spread or in haycocks on the ground. Here and there, on soil close to the river’s brink, the masses of purple loosestrife made a glow of colour;or in some uncut field where the grass was short and brown the dark red cows were pasturing quietly; or now and then one, unconsciously[2] picturesque, would be standing on the bank of the river, a distinct picture there. The train
steamed onwards with its scanty freight of passengers, between the lines of the river and the canal, in the midst of the quiet fields and the mellow evening light.
10-10:常见单词 访问古登堡计划,⼀些你想分析的图书。下载这些作品的⽂本⽂件或将浏览器中的原始⽂本复制到⽂本⽂件中。可以使⽤⽅法count()来确定特定的单词或短语在字符串中出现了多少次。例如,下⾯的代码计算’row’在⼀个字符串中出现了多少次:line = “Row, row, row your boat”
2
line.lower().count(‘row’)
3
请注意,通过使⽤lower() 将字符串转换为⼩写,可捕捉要查单词的所有格式,⽽不管其⼤⼩写如何。编写⼀个程序,它读取你在古登堡计划中获取的⽂件,并计算单词’the’ 在每个⽂件中分别出现了多少次。这⾥计算得到的结果并不准确,因为将诸如’then’
和’there’ 等单词也计算在内了。请尝试计算’the '(包含空格)出现的次数,看看结果相差多少。
10-11 喜欢的数 编写⼀个程序,提⽰⽤户输⼊喜欢的数,并使⽤json.dump() 将这个数存储到⽂件中。再编写⼀个程序,从⽂件中读取这个值,并打印如下所⽰的消息。 #I know your favorite number! It’s _____.
10-12 记住喜欢的数 将练习10-11中的程序合⼆为⼀。如果存储了⽤户喜欢的数,就向⽤户显⽰它,否则提⽰⽤户输⼊喜欢的数并将其存储到⽂件中。运⾏这个程序两次,看看它能否像预期的那样⼯作。the_obj = 0book = 'Jenny A 'with  open (book ,encoding ='utf-8') as  f : lines = f .readlines () for  line in  lines :  content = line .lower ().count ('the ')  the_obj += content  print (f"'the '出现{the_obj }次")
1
2
网页素材资源网站
3
4
5
6
7
8import  json number = input ('请输⼊喜欢的数:')filename = '10.11.json'with  open ('10.11.json','w',encoding ='utf-8') as  f : json .dump (number ,f )with  open ('10.11.json',encoding ='utf-8') as  f : content = json .load (f ) print (f"I know your favorite number! It's {content }")
1
2
3
4python基础代码练习
5
6
7
8
9import  json filename = '10.11.json'try : with  open (filename ) as  f :  num = json .load (f )  except  FileNotFoundError : num = input ('请输⼊喜欢的数:') with  open (filename ,'w',encoding ='utf-8') as  f :  json .dump (num ,f )  print (f"We'll remember the number of {num }")else : print (f"There is your favorite number:{num }")
1
2
3
4
5
6
7
8
9
10
11
12
13
username.json
10-13 验证⽤户 最后⼀个remember_me.py版本假设⽤户要么已输⼊⽤户名,要么是⾸次运⾏该程序。
我们应该修改这个程序,以防当前⽤户并⾮上次运⾏该程序的⽤户。为此,在greet_user() 中打印欢迎⽤户回来的消息前,询问他⽤户名是否正确。如果不对,就调⽤get_new_username()让⽤户输⼊正确的⽤户名。{"username":"Eric"}
1import  json def  get_stored_username (): """如果存储了⽤户名,就获取它。""" filename = 'username.json' try :  with  open (filename ) as  f :  username = json .load (f ) except  FileNotFoundError :  return  None  else :  return  username def  get_new_username (): """提⽰⽤户输⼊⽤户名。""" username = input ("What is your name? ") filename = 'username.json' with  open (filename , 'w') as  f :  json .dump (username , f )  return  username def  greet_user (): """问候⽤户,并指出其名字。""" username = get_stored_username () print (f'您的⽤户名是否为:{username }?') user = input ('正确请输⼊Y ,错误请输⼊N :') if  user == 'Y':  print (f"Welcome back, {username }!") else :  username = get_new_username ()  print (f"您的正确⽤户名是:{username }!")greet_user ()1
2
siblings是什么意思中文3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
c语言干嘛用的
27
28
29
30
31
32

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。