├── Python编程:从入门到实践.pdf ├── README.md ├── python_10th_answers ├── 10th_1-13.py ├── cats.txt ├── dogs.txt ├── favorite_number.txt ├── guest.txt ├── learning_python.txt ├── lovenumber.json ├── reasons.txt ├── username.json └── words_count.txt ├── python_11th_answers ├── 11th_1-3.py ├── __pycache__ │ ├── city_functions.cpython-36.pyc │ ├── cityfunctions_population.cpython-36.pyc │ └── employee.cpython-36.pyc ├── city_functions.py └── employee.py ├── python_4th_answers └── 4th_4-13.py ├── python_5th_answers ├── 5th_2.py └── 5th_8-11.py ├── python_6th_answers └── 6th_1-11.py ├── python_7th_answers └── 7th_1-10.py ├── python_8th_answers ├── 8th_1-17.py ├── __pycache__ │ ├── print_helloworld.cpython-36.pyc │ └── printing_function.cpython-36.pyc ├── print_helloworld.py └── printing_function.py ├── python_9th_answers ├── 9th_1-15.py ├── __pycache__ │ ├── Restaurant.cpython-36.pyc │ ├── admin_privileges.cpython-36.pyc │ ├── user.cpython-36.pyc │ └── user_only.cpython-36.pyc ├── admin_privileges.py ├── restaurant.py ├── user.py └── user_only.py └── test.txt /Python编程:从入门到实践.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/Python编程:从入门到实践.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LearnPython 2 | Some python answer codes for “python编程从入门到实践” by LSayhi 3 | -------------------------------------------------------------------------------- /python_10th_answers/10th_1-13.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Dec 6 20:15:44 2017 4 | 5 | @author: LSayhi 6 | """ 7 | #homework for python 10th 8 | #10-1 9 | ''' 10 | file_name='learning_python.txt' 11 | with open(file_name) as lp: 12 | contents=lp.read() 13 | print(contents.rstrip()) 14 | with open(file_name) as lp: 15 | for line in lp: 16 | print(line.rstrip()) 17 | with open(file_name) as lp: 18 | lines=lp.readlines() 19 | for line in lines: 20 | print(line.strip()) 21 | ''' 22 | #10-2 23 | ''' 24 | file_name='learning_python.txt' 25 | with open(file_name) as lp: 26 | for line in lp: 27 | print(line.replace('python','C++').rstrip()) 28 | ''' 29 | #10-3 30 | ''' 31 | class Guest_input(): 32 | """访客类""" 33 | def __init__(self,name,age): 34 | self.name=name 35 | self.age=age 36 | guest_name=input('please input your name: ') 37 | guest_age =input('please input your age : ') 38 | guest=Guest_input(guest_name,guest_age) 39 | with open('guest.txt','w') as file_obj: 40 | file_obj.write(guest.name+'\n') 41 | file_obj.write(guest.age+'\n') 42 | with open('guest.txt','r') as file_obj: 43 | fo=file_obj.read() 44 | print(fo) 45 | ''' 46 | #10-4 47 | ''' 48 | class Guest_input(): 49 | """访客类""" 50 | def __init__(self,name,age): 51 | self.name=name 52 | self.age=age 53 | guest_name='' 54 | guest_name='' 55 | while True: 56 | guest_name=str(input("please input your name('quit' to break): ")) 57 | if guest_name=='quit': 58 | break 59 | guest_age =str(input("please input your age ('quit' to break): ")) 60 | if guest_name=='quit': 61 | break 62 | guest=Guest_input(guest_name,guest_age) 63 | with open('guest.txt','a') as file_obj: 64 | file_obj.write(guest.name+' ') 65 | file_obj.write(guest.age+'\n') 66 | with open('guest.txt','r') as file_obj: 67 | fo=file_obj.read() 68 | print(fo) 69 | ''' 70 | #10-5 71 | ''' 72 | while True: 73 | reason=str(input("Why you love programing?(q to quit):")) 74 | if reason =='q': 75 | break 76 | with open('reasons.txt','a') as file_obj: 77 | file_obj.write(reason) 78 | with open('reasons.txt') as rea: 79 | print(rea.read()) 80 | ''' 81 | #10-6 82 | ''' 83 | def two_sum(numa,numb): 84 | """两个数的和""" 85 | try: 86 | print(int(numa)+int(numb)) 87 | except: 88 | print("What you input are not two numbers!") 89 | a=input('plese input a number:') 90 | b=input('plese input anther number:') 91 | two_sum(a,b) 92 | ''' 93 | #10-7 94 | ''' 95 | def two_sum(numa,numb): 96 | """两个数的和""" 97 | try: 98 | c=int(numa)+int(numb) 99 | except: 100 | pass 101 | else: 102 | print(c) 103 | while True: 104 | a=input('plese input a number(q to qiut):') 105 | b=input('plese input anther number(q to quit):') 106 | if(a=='q')or(b=='q'): 107 | break 108 | two_sum(a,b) 109 | ''' 110 | #10-8 111 | ''' 112 | try : 113 | with open('cats.txt') as cats_obj: 114 | cats=cats_obj.read() 115 | with open('dogs.txt') as dogs_obj: 116 | dogs=dogs_obj.read() 117 | except: 118 | print('file not found') 119 | else: 120 | print(cats) 121 | print(dogs) 122 | ''' 123 | #10-9 124 | ''' 125 | try : 126 | with open('cats.txt') as cats_obj: 127 | cats=cats_obj.read() 128 | with open('dogs.txt') as dogs_obj: 129 | dogs=dogs_obj.read() 130 | except: 131 | pass 132 | else: 133 | print(cats) 134 | print(dogs) 135 | ''' 136 | #10-10 137 | ''' 138 | try: 139 | with open('words_count.txt') as file_obj: 140 | lines=file_obj.readlines() 141 | except: 142 | print('file not found') 143 | else: 144 | a=0 145 | for line in lines: 146 | a+=line.lower().count('the') 147 | print(a) 148 | ''' 149 | #10-11 150 | ''' 151 | import json 152 | filename='favorite_number.json' 153 | favorite_num=input('please input your favorite number: ') 154 | with open(filename,'w') as file_obj: 155 | json.dump(favorite_num,file_obj) 156 | with open(filename,'r') as f_obj: 157 | f_n=json.load(f_obj) 158 | print("I know your favorite number! It's "+f_n) 159 | ''' 160 | #10-12 161 | ''' 162 | import json 163 | def get_stored_number(): 164 | """返回最喜欢的数字""" 165 | filename='lovenumber.json' 166 | try: 167 | with open(filename) as f_obj: 168 | favorite_number=json.load(f_obj) 169 | except FileNotFoundError: 170 | return None 171 | else: 172 | return favorite_number 173 | def new_favorite_number(): 174 | """提示并存储最喜欢的数字""" 175 | favorite_number=input("What's your favorite number?: ") 176 | filename='lovenumber.json' 177 | with open(filename,'w') as f_obj: 178 | json.dump(favorite_number,f_obj) 179 | return favorite_number 180 | def show_number(): 181 | """显示最喜欢的数字""" 182 | favorite_number=get_stored_number() 183 | if favorite_number: 184 | print("I know your favorite number! It's "+favorite_number) 185 | else: 186 | favorite_number=new_favorite_number() 187 | show_number() 188 | ''' 189 | #10-13 190 | ''' 191 | import json 192 | def get_stored_username(): 193 | """如果用户名存在,则获取用户名""" 194 | filename='username.json' 195 | try: 196 | with open(filename) as f_obj: 197 | user_name=json.load(f_obj) 198 | except FileNotFoundError: 199 | return None 200 | else: 201 | return user_name 202 | def get_new_username(): 203 | """提示输入用户名并存储返回""" 204 | user_name=input("so,what's your name?: ") 205 | filename='username.json' 206 | with open(filename,'w') as f_obj: 207 | json.dump(user_name,f_obj) 208 | return user_name 209 | def greet_user(): 210 | """询问用户名,并提示""" 211 | user_name=get_stored_username() 212 | label=input('Welcome,is that '+"'"+user_name+"'"+' your name?(y/n):') 213 | if label=='y': 214 | print("Wecome back "+user_name) 215 | else: 216 | user_name=get_new_username() 217 | print("well,I will remrember you when you come back") 218 | greet_user() 219 | ''' 220 | -------------------------------------------------------------------------------- /python_10th_answers/cats.txt: -------------------------------------------------------------------------------- 1 | cat1 cat2 cat3 -------------------------------------------------------------------------------- /python_10th_answers/dogs.txt: -------------------------------------------------------------------------------- 1 | dog1 dog2 dog3 -------------------------------------------------------------------------------- /python_10th_answers/favorite_number.txt: -------------------------------------------------------------------------------- 1 | "19" -------------------------------------------------------------------------------- /python_10th_answers/guest.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /python_10th_answers/learning_python.txt: -------------------------------------------------------------------------------- 1 | In python you can be efficient 2 | In python you can use list simplely 3 | In python you can use class to describe things -------------------------------------------------------------------------------- /python_10th_answers/lovenumber.json: -------------------------------------------------------------------------------- 1 | "20" -------------------------------------------------------------------------------- /python_10th_answers/reasons.txt: -------------------------------------------------------------------------------- 1 | It gives me happeness -------------------------------------------------------------------------------- /python_10th_answers/username.json: -------------------------------------------------------------------------------- 1 | "LSayhi" -------------------------------------------------------------------------------- /python_10th_answers/words_count.txt: -------------------------------------------------------------------------------- 1 | Free ebooks - Project Gutenberg 2 | Book search · Book categories · Browse catalog · Mobile site · Report errors · Terms of use 3 | 4 | Some of the Latest Books 5 | 6 | 7 | 8 | 9 | Welcome 10 | 11 | QR Code 12 | Project Gutenberg Mobile Site 13 | Project Gutenberg offers over 54,000 free eBooks: Choose among free epub books, free kindle books, download them or read them online. You will find the world's great literature here, especially older works for which copyright has expired. We digitized and diligently proofread them with the help of thousands of volunteers. 14 | 15 | No fee or registration is required, but if you find Project Gutenberg useful, we kindly ask you to donate a small amount so we can digitize more books, maintain our online presence, and improve Project Gutenberg programs and offerings. Other ways to help include digitizing more books, recording audio books, or reporting errors. 16 | 17 | News 18 | 19 | Project Gutenberg Supports Net Neutrality 20 | The Federal Communications Commission in the US is considering abandoning major components of network neutrality. This would legitimize "slow lanes" for network traffic that does not come from commercial partners of network providers. Sites where content is free, and generates no revenue - such as Project Gutenberg - would be at risk for downgraded speeds, access fees imposed by your network provider, or ads embedded by your network provider. Comments are solicited by the FCC at www.fcc.gov. Project Gutenberg encourages you to send messages to the FCC and your lawmakers, to express your views on this important issue. 21 | 22 | The Public Domain will grow again in 2019 23 | In the US, annual copyright term expiry is set to begin again in 2019, after a 20-year hiatus due to the Copyright Term Extension Act of 1998. On January 1, 2019, items published in 1923 will enter the public domain in the US. In the early days of Project Gutenberg, growth of the public domain on January 1 was an annual event. See Duke Law's "Public Domain Day"for a listing of many items that were scheduled to enter the public domain, but have yet to do so because of the 1998 extension. Some notable items scheduled to enter the public domain in 2019 include Felix Salten's "Bambi" and Kahlil Gibran's "The Prophet." 24 | 25 | Site Map 26 | 27 | Find eBooks 28 | Book Search. 29 | Recently added eBooks. 30 | Most Frequently Downloaded eBooks and Top 100 eBooks this month. 31 | Bookshelves of related eBooks. 32 | New Books Feeds. 33 | Browse Catalog: Browse and search, including full-text search. 34 | Offline Catalogs: handy book Listings to consult offline. 35 | Visit self.gutenberg.org, for free eBooks by contemporary authors. 36 | Help and Information 37 | Mobile Reader Devices How-To: Using Kindle, Nook, cell phone, and other mobile devices and readers. 38 | How-To and FAQs: In depth information about many topics. 39 | How to Help 40 | Report errors, bugs, typos (or, see detailed information about errata reporting) 41 | Volunteering. 42 | Distributed Proofreaders. Getting started is easy, and just a page a day will help! 43 | LibriVox. Help record audio books. 44 | Help to promote Project Gutenberg 45 | Special areas 46 | About Us: About Project Gutenberg. 47 | No Cost or Freedom? What does 'free eBook' mean? 48 | Permissions, licensing and trademark information. 49 | Linking Readme: Linking to Project Gutenberg. 50 | Robot Readme: Downloading many items at once. 51 | Donate: Donating to Project Gutenberg. 52 | News and Newsletters: (External) News and information about Project Gutenberg. 53 | Partners, Affiliates and Resources. 54 | Terms of Use 55 | 56 | Our eBooks may be freely used in the United States because most are not protected by U.S. copyright law, usually because their copyrights have expired. They may not be free of copyright in other countries. Readers outside of the United States must check the copyright terms of their countries before downloading or redistributing our eBooks. We also have a number of copyrighted titles, for which the copyright holder has given permission for unlimited non-commercial worldwide use. 57 | 58 | The Project Gutenberg website is for human users only. Any real or perceived use of automated tools to access our site will result in a block of your IP address. This site utilizes cookies, captchas and related technologies to help assure the site is maximally available for human users only. 59 | 60 | For more details see our Terms of Use page. 61 | 62 | Contact Info 63 | 64 | Contact Information: How to get in touch. 65 | Mailing lists: Join our email lists. -------------------------------------------------------------------------------- /python_11th_answers/11th_1-3.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Dec 8 22:48:33 2017 4 | 5 | @author: LSayhi 6 | """ 7 | #homework for python 11th 8 | #11-1 test_cities.py 9 | ''' 10 | from city_functions import city_of_country 11 | import unittest 12 | class CityCountryTest(unittest.TestCase): 13 | """"测试city_functions.py""" 14 | def test_city_country(self): 15 | string=city_of_country('Santiago','Chile') 16 | self.assertEqual(string,'Santiago,Chile') 17 | unittest.main() 18 | ''' 19 | #11-2 20 | ''' 21 | from city_functions import city_of_country 22 | from city_functions import city_of_country_option 23 | from city_functions import city_of_country_population 24 | import unittest 25 | class CityCountryTest(unittest.TestCase): 26 | """"测试city_functions_*.py""" 27 | def test_city_country(self): 28 | string=city_of_country('Santiago','Chile') 29 | self.assertEqual(string,'Santiago,Chile') 30 | def test_city_country_population(self): 31 | string=city_of_country_population('Santiago','Chile','10000') 32 | self.assertEqual(string,'Santiago,Chile- population 10000') 33 | def test_city_country_option(self): 34 | stringa=city_of_country_option('Santiago','Chile','10000') 35 | self.assertEqual(stringa,'Santiago,Chile- population 10000') 36 | stringb=city_of_country('Santiago','Chile') 37 | self.assertEqual(stringb,'Santiago,Chile') 38 | unittest.main() 39 | ''' 40 | #11-3 41 | ''' 42 | from employee import Employee 43 | import unittest 44 | class TestEmployee(unittest.TestCase): 45 | """测试Employee类""" 46 | def setUp(self): 47 | self.employeetest=Employee('Liu','jiangfan',1111) 48 | def test_give_default_rasie(self): 49 | self.assertEqual(self.employeetest.give_raise(),6111) 50 | def test_give_custom_rasie(self): 51 | self.assertEqual(self.employeetest.give_raise(8888),9999) 52 | unittest.main() 53 | ''' -------------------------------------------------------------------------------- /python_11th_answers/__pycache__/city_functions.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_11th_answers/__pycache__/city_functions.cpython-36.pyc -------------------------------------------------------------------------------- /python_11th_answers/__pycache__/cityfunctions_population.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_11th_answers/__pycache__/cityfunctions_population.cpython-36.pyc -------------------------------------------------------------------------------- /python_11th_answers/__pycache__/employee.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_11th_answers/__pycache__/employee.cpython-36.pyc -------------------------------------------------------------------------------- /python_11th_answers/city_functions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Dec 8 23:08:06 2017 4 | 5 | @author: LSayhi 6 | """ 7 | 8 | def city_of_country(city,country): 9 | """返回城市和国家的字符串""" 10 | string=city.title()+','+country.title() 11 | return string 12 | 13 | def city_of_country_population(city,country,population): 14 | """返回城市、国家、人口的字符串""" 15 | string=city.title()+','+country.title()+'- '+'population '+population 16 | return string 17 | 18 | def city_of_country_option(city,country,population=''): 19 | """返回城市、国家(人口可选)的字符串""" 20 | string=city.title()+','+country.title()+'- '+'population '+population 21 | return string -------------------------------------------------------------------------------- /python_11th_answers/employee.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Dec 8 23:53:52 2017 4 | 5 | @author: LSayhi 6 | """ 7 | 8 | class Employee(): 9 | """创建雇员信息,返回年薪""" 10 | def __init__(self,firstname,lastname,year_rasie): 11 | self.firstname=firstname 12 | self.lastname =lastname 13 | self.year_rasie=year_rasie 14 | def give_raise(self,new_rasie=5000): 15 | self.year_rasie+=int(new_rasie) 16 | return (self.year_rasie) 17 | -------------------------------------------------------------------------------- /python_4th_answers/4th_4-13.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Nov 18 21:41:56 2017 4 | 5 | @author: LSayhi 6 | """ 7 | 8 | #python编程从入门到实践 4-13 9 | foods=('food1','food2','food3','food4','food5') 10 | for food in foods: 11 | print(food) 12 | print('The new foods are :') 13 | foods=('food1','food2','foodA','foodB','food5') 14 | for food in foods: 15 | print(food) 16 | #foods[1]='foodMM' -------------------------------------------------------------------------------- /python_5th_answers/5th_2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Nov 18 22:43:10 2017 4 | 5 | @author: LSayhi 6 | """ 7 | 8 | #homework 5-2 in python 9 | #1 10 | print('1th :\nplease input two strings:\n') 11 | name1=input() 12 | name2=input() 13 | print('if they are the same?\n') 14 | if name1==name2: 15 | print('\tthey are equal!\n') 16 | else: 17 | print('\tthey are not equal\n') 18 | print('if they are the same after lower?\n') 19 | if name1.lower()==name2.lower(): 20 | print('\tthey are equal!\n') 21 | else: 22 | print('\tthey are not equal\n') 23 | #2 24 | print('2th :\nplease input a string:\n') 25 | list=['apple','red','bule'] 26 | string=input() 27 | if string in list: 28 | print('The input string is in the very list\n') 29 | else: 30 | print('The string is not in the very list\n') -------------------------------------------------------------------------------- /python_5th_answers/5th_8-11.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Nov 19 23:05:55 2017 4 | 5 | @author: LSayhi 6 | """ 7 | #homework for python 5-8,……,5-11 8 | #5-8 9 | ''' 10 | user_names=['bu','yao','shuo','admin','shi','tian','cai'] 11 | for user in user_names: 12 | if user =='admin': 13 | print('hello admin,would you like to see a status report?') 14 | else: 15 | print('hello'+user+',thank you for logging in again.') 16 | ''' 17 | #5-9 18 | ''' 19 | user_names=['bu','yao','shuo','admin','shi','tian','cai'] 20 | if user_names: 21 | for user in user_names: 22 | if user =='admin': 23 | print('hello admin,would you like to see a status report?') 24 | else: 25 | print('hello'+user+',thank you for logging in again.') 26 | else: 27 | print('We need to find some users.') 28 | user_names=[] 29 | if not user_names: 30 | print('we need to find some usres.') 31 | ''' 32 | #5-10 33 | ''' 34 | current_users=['bu','yao','shuo','wo','shi','tian','cai','HI'] 35 | i=0 36 | for current_user in current_users: 37 | current_users[i]=current_user.lower() 38 | i=i+1 39 | new_users=['Bu','bili','shuo','hi','helloworld'] 40 | for new_user in new_users: 41 | if new_user.lower() in current_users: 42 | print("'"+new_user+"'"+" is already exist,please input other's name") 43 | else: 44 | print("'"+new_user+"'"+' can be used.') 45 | ''' 46 | #5-11 47 | numbers=[1,2,3,4,5,6] 48 | for number in numbers: 49 | if number == 1: 50 | print(str(number)+'st') 51 | elif number == 2: 52 | print(str(number)+'nd') 53 | elif number == 3: 54 | print(str(number)+'rd') 55 | else: 56 | print(str(number)+'th') 57 | -------------------------------------------------------------------------------- /python_6th_answers/6th_1-11.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Nov 20 16:35:16 2017 4 | 5 | @author: LSayhi 6 | """ 7 | 8 | #homework for python 6_1~6_11 9 | #6-1 10 | ''' 11 | Liujiangfan={'first_name':'jiangfan', 12 | 'last_name':'Liu', 13 | 'age':'22', 14 | 'city':'Beijing' 15 | } 16 | for key,value in Liujiangfan.items(): 17 | print("Liujiangfan's "+key+' is '+value) 18 | ''' 19 | #6-2 20 | ''' 21 | lovenumber={'liujiangfan':'6', 22 | 'liujiaqi':'5', 23 | 'liuyu':'2', 24 | 'liubei':'3', 25 | 'guanyu':'7', 26 | } 27 | for name,number in lovenumber.items(): 28 | print('\t'+name+"'s favorite number is "+number) 29 | ''' 30 | #6-3 31 | ''' 32 | words={'list':'A series of paraments sorted in order ', 33 | 'if':'assume', 34 | 'title':'name of something', 35 | 'a':'before b', 36 | 'c':'after b' 37 | } 38 | for name,meaning in words.items(): 39 | print(' '+name+':\n\t'+meaning) 40 | ''' 41 | #6-4 42 | ''' 43 | words={'list ':'A series of paraments sorted in order ', 44 | 'if ':'assume', 45 | 'title':'name of something', 46 | 'a ':'before b', 47 | 'c ':'after b' 48 | } 49 | words['while']="return 'true' if expresstion in 'while' is satisified" 50 | words['elif '] ='use after if' 51 | words['else '] ='use after if or elif' 52 | words['for '] ='use to express loop' 53 | words['# '] ='use to express notes' 54 | for name,meaning in words.items(): 55 | print(' '+name+':\t'+meaning) 56 | ''' 57 | #6-5 58 | ''' 59 | riverflow={'changjiang ':['hubei','jiangxi','zhejiang',], 60 | 'huanghe ':['ganshu','henan','shanghai'], 61 | 'heilongjiang':['neimenggu','heilongprovince'], 62 | } 63 | for river,locations in riverflow.items(): 64 | print(river.title()+' runs through: ') 65 | for location in locations: 66 | print('\t'+location) 67 | ''' 68 | #6-6 69 | ''' 70 | favorite_languages={'jen':'python', 71 | 'sarah':'c', 72 | 'phil':'java', 73 | } 74 | namelist=['jen','liujiangfan','Phil','liuyu'] 75 | for name in namelist: 76 | if name.lower() in favorite_languages.keys(): 77 | print('You have taken part in this survey,thanks!') 78 | else: 79 | print('please start this survey,thank you.') 80 | ''' 81 | #6-7 82 | ''' 83 | Liujiangfan={'first_name':'jiangfan', 84 | 'last_name':'Liu', 85 | 'age':'22', 86 | 'city':'Beijing' 87 | } 88 | Liuyu={'first_name':'yu', 89 | 'last_name':'Liu', 90 | 'age':'22', 91 | 'city':'Shangrao' 92 | } 93 | Liujiaqi={'first_name':'jiaqi', 94 | 'last_name':'Liu', 95 | 'age':'20', 96 | 'city':'Nanchang' 97 | } 98 | people=[Liujiangfan,Liuyu,Liujiaqi] 99 | for person in people: 100 | for key,value in person.items(): 101 | print(person['last_name']+person['first_name']+"'s "+ 102 | key+' is '+ value) 103 | print('\n') 104 | ''' 105 | #6-8 106 | ''' 107 | cat={'type':'burudongwu', 108 | 'owner_name':'Liujiangfan', 109 | } 110 | pstitaciforme={'type':'birds', 111 | 'owner_name':'Liuyu' 112 | } 113 | pets=[cat,pstitaciforme] 114 | for pet in pets: 115 | for key,value in pet.items(): 116 | print( 117 | key+' is '+ value) 118 | print('\n') 119 | ''' 120 | #6-9,重复,略 121 | #6-10重复,略 122 | #6-11 123 | ''' 124 | cities={ 125 | 'Beijing ':{'country':'China', 126 | 'population':'21.70 million', 127 | 'fact':'capital of China' 128 | }, 129 | 'Shanghai':{'country':'China', 130 | 'population':'23.61 million', 131 | 'fact':'financial center of China' 132 | }, 133 | 'tokyo ':{'country':'Japan', 134 | 'population':'42.00 million', 135 | 'fact':'capital of Japan' 136 | } 137 | } 138 | #city stores the city's name,information stores the very city's information 139 | for city,information in cities.items(): 140 | print(city+' belongs to '+information['country']+', '+ 141 | 'the population is '+information['population']+ 142 | ', the '+information['fact']+'.' 143 | ) 144 | ''' 145 | -------------------------------------------------------------------------------- /python_7th_answers/7th_1-10.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Nov 21 17:01:36 2017 4 | 5 | @author: LSayhi 6 | """ 7 | 8 | #homework for python 7_1 ~ 7-10 9 | #7-1 10 | ''' 11 | car=input('What kind of car do you want to rent? ') 12 | print('Let me see if i can find you a '+car) 13 | ''' 14 | #7-2 15 | ''' 16 | number=input('How many people are watting for your foods? ') 17 | number=int(number) 18 | if number>8: 19 | print('sorry,there is no empty table.') 20 | else: 21 | print('weclome,there are some tables.') 22 | ''' 23 | #7-3 24 | ''' 25 | number=input('please input a number: ') 26 | number=int(number) 27 | if number%10 == 0: 28 | print(str(number)+' is '+str(int(number/10))+' times of 10.') 29 | else: 30 | print(str(number)+' is not any integer multiples of 10.') 31 | ''' 32 | #7-4 33 | ''' 34 | print("please input foods you wish, input 'quit' to exit if you want") 35 | foods=[] 36 | active=1 37 | while active: 38 | food=input() 39 | if food != 'quit': 40 | print(food+' has been added. ') 41 | foods.append(str(food)) 42 | else: 43 | active=0 44 | print('The foods you wish is/are: ') 45 | for food in foods: 46 | print(food,end=',') 47 | ''' 48 | #7-5 49 | ''' 50 | age=1 51 | while age: 52 | age=int(input('please input your age(1-200 years,0 to quit):')) 53 | if age <3 and age >0: 54 | print('free to wantch') 55 | elif age>=3 and age<=12: 56 | print('you need pay 10 dollars') 57 | elif age>12: 58 | print('you need pay 15 dollars') 59 | ''' 60 | #7-6 61 | ''' 62 | print("please input foods you wish, input 'quit' to exit if you want") 63 | foods=[] 64 | while True: 65 | food=input() 66 | if food != 'quit': 67 | print(food+' has been added. ') 68 | foods.append(str(food)) 69 | else: 70 | break 71 | print('The foods you wish is/are: ') 72 | for food in foods: 73 | print(food,end=',') 74 | ''' 75 | #7-7 76 | ''' 77 | while True: 78 | print('busy') 79 | ''' 80 | #7-8 81 | ''' 82 | swh='sanwich' 83 | sanwich_orders=['tomato-'+swh,'meat-'+swh,'cheese-'+swh] 84 | finished_sandwiches=[] 85 | while sanwich_orders: 86 | sanwich_temp=sanwich_orders.pop() 87 | print('I made you '+sanwich_temp) 88 | finished_sandwiches.append(sanwich_temp) 89 | print('sanwiches list:') 90 | for sanwich in finished_sandwiches: 91 | print(sanwich) 92 | ''' 93 | #7-9 94 | ''' 95 | swh='sanwich' 96 | sanwich_orders=['tomato-'+swh,'meat-'+swh,'pastrami'+swh,'cheese-'+swh, 97 | 'pastrami'+swh,'mayo'+swh,'pastrami'+swh] 98 | while ('pastrami'+swh) in sanwich_orders: 99 | sanwich_orders.remove('pastrami'+swh) 100 | print(sanwich_orders) 101 | ''' 102 | #7-10 103 | ''' 104 | name_place={} 105 | active=True 106 | while active: 107 | name =input("What's your name:") 108 | place=input('where do you want to viste:') 109 | name_place[name]=place 110 | flag=input('Is there anyone others want to join?(y/n): ') 111 | #if flag != 'y' or 'yes': 112 | if flag !=('y' or 'yes'): 113 | active=False 114 | for name,place in name_place.items(): 115 | print(name.title()+' want to viste '+place) 116 | ''' -------------------------------------------------------------------------------- /python_8th_answers/8th_1-17.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Nov 23 19:58:46 2017 4 | 5 | @author: LSayhi 6 | """ 7 | #homework for python 8th lecture 8 | #8-1 9 | ''' 10 | def display_msg(): 11 | print(' You will learn "function" in this lecture ') 12 | display_msg() 13 | ''' 14 | #8-2 15 | ''' 16 | def favorite_book(title): 17 | print('One of my favorite book is '+title.title()) 18 | favorite_book('《Python编程从入门到实践》' ) 19 | ''' 20 | #8-3 21 | ''' 22 | def make_shirt(size,word): 23 | print('Size is '+size+', word is '+"'"+word+"'") 24 | make_shirt('L','we are BYR') 25 | make_shirt(word='Best',size='XXL') 26 | ''' 27 | #8-4 28 | ''' 29 | def make_shirt(size='Large',word='I love python'): 30 | print('Size is '+size+', word is '+"'"+word+"'") 31 | make_shirt() 32 | make_shirt('middle') 33 | make_shirt('XL','we are JUFE') 34 | ''' 35 | #8-5 36 | ''' 37 | def describe_city(city,country='Iceland'): 38 | print(city+' is in '+country) 39 | describe_city('Reykjavik') 40 | describe_city(city='Beijing',country='China') 41 | describe_city('Tokyo','Japan') 42 | ''' 43 | #8-6 44 | ''' 45 | def describe_city(city,country): 46 | message='"'+city+', '+country+'"' 47 | return message 48 | msg=describe_city('Reykjavik','Iceland') 49 | print(msg) 50 | msg=describe_city(city='Beijing',country='China') 51 | print(msg) 52 | msg=describe_city('Tokyo','Japan') 53 | print(msg) 54 | ''' 55 | #8-7 56 | ''' 57 | def make_album(name,album,songsnum=''): 58 | name_album={} 59 | name_album['name'] =name 60 | name_album['album']=album 61 | if songsnum: 62 | name_album['songnum']=songsnum 63 | return name_album 64 | print(make_album('周杰伦','范特西')) 65 | print(make_album('周杰伦','七里香','10')) 66 | print(make_album('周杰伦','哎呦不错呦','12')) 67 | ''' 68 | #8-8 69 | ''' 70 | def make_album(name='',album='',songsnum=''): 71 | name_album={} 72 | if name: 73 | name_album['name'] =name 74 | if album: 75 | name_album['album']=album 76 | if songsnum: 77 | name_album['songnum']=songsnum 78 | return name_album 79 | while True: 80 | name=input("please input star's name: ") 81 | if (name=='quit'): 82 | break 83 | ablum=input("ablum name: ") 84 | if (ablum=='quit'): 85 | break 86 | songsnum=input("the ablum song numbers: ") 87 | if (songsnum=='quit'): 88 | break 89 | print(make_album(name,ablum,songsnum)) 90 | ''' 91 | #8-9 92 | ''' 93 | def show_magicans(magcianlist): 94 | for magican in magcianlist: 95 | print(magican) 96 | magicans=['Divad','Liuqian','JackMa'] 97 | show_magicans(magicans) 98 | ''' 99 | #8-10 100 | ''' 101 | def make_great(magicanlist): 102 | for i in range(len(magicanlist)): 103 | magicanlist[i]='the Great '+magicanlist[i] 104 | def show_magicans(pmagicanlist): 105 | for pmagican in pmagicanlist: 106 | print(pmagican) 107 | magicans=['Divad','Liuqian','JackMa'] 108 | make_great(magicans) 109 | show_magicans(magicans) 110 | ''' 111 | #8-11 112 | ''' 113 | def make_great(magicanlist): 114 | for i in range(len(magicanlist)): 115 | magicanlist[i]='the Great '+magicanlist[i] 116 | return magicanlist 117 | def show_magicans(pmagicanlist): 118 | for pmagican in pmagicanlist: 119 | print(pmagican) 120 | magicans=['Divad','Liuqian','JackMa'] 121 | magicanss=make_great(magicans[:]) 122 | show_magicans(magicans) 123 | show_magicans(magicanss) 124 | ''' 125 | #8-12 126 | ''' 127 | def make_sandwich(*gredients): 128 | print(gredients) 129 | make_sandwich('mushroom') 130 | make_sandwich('mushiroom','cheese') 131 | make_sandwich('mushiroom','cheese','peppers') 132 | ''' 133 | #8-13 134 | ''' 135 | def build_profile(first,last,**user_info): 136 | profile={} 137 | profile['fisrt_name']=first 138 | profile['last_name'] =last 139 | for key,value in user_info.items(): 140 | profile[key]=value 141 | return profile 142 | user_profile=build_profile('jiangfan','liu',age='17', 143 | location='beijing',field='EE') 144 | print(user_profile) 145 | ''' 146 | #8-14 147 | ''' 148 | def make_car(manufacturer,version,**car_info): 149 | carinfo={} 150 | carinfo['manufacturer']=manufacturer 151 | carinfo['version'] =version 152 | for key,value in car_info.items(): 153 | carinfo[key]=value 154 | return carinfo 155 | car_profile=make_car('BMW','3',color='blue', 156 | maxspeed='350km/h') 157 | print(car_profile) 158 | ''' 159 | #8-15 160 | ''' 161 | from printing_function import print_models as pm 162 | from printing_function import show_completed_models as scm 163 | unprinted_designs=['iphone case','robot pendant','dodecahedron'] 164 | completed_models=[] 165 | pm(unprinted_designs,completed_models) 166 | scm(completed_models) 167 | ''' 168 | #8-16 169 | ''' 170 | import print_helloworld 171 | print_helloworld.helloworld() 172 | ''' 173 | ''' 174 | from print_helloworld import helloworld 175 | helloworld() 176 | ''' 177 | ''' 178 | from print_helloworld import helloworld as hw 179 | hw() 180 | ''' 181 | ''' 182 | import print_helloworld as phw 183 | phw.helloworld() 184 | ''' 185 | ''' 186 | from print_helloworld import * 187 | helloworld() 188 | ''' 189 | #8-17 190 | '''see as above''' -------------------------------------------------------------------------------- /python_8th_answers/__pycache__/print_helloworld.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_8th_answers/__pycache__/print_helloworld.cpython-36.pyc -------------------------------------------------------------------------------- /python_8th_answers/__pycache__/printing_function.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_8th_answers/__pycache__/printing_function.cpython-36.pyc -------------------------------------------------------------------------------- /python_8th_answers/print_helloworld.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Dec 2 22:33:26 2017 4 | 5 | @author: LSayhi 6 | """ 7 | 8 | def helloworld(): 9 | print('hello world') 10 | -------------------------------------------------------------------------------- /python_8th_answers/printing_function.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Dec 2 21:06:34 2017 4 | 5 | @author: LSayhi 6 | """ 7 | 8 | def print_models(unprinted_designs,completed_models): 9 | while unprinted_designs: 10 | current_design=unprinted_designs.pop() 11 | print('printing model: '+current_design) 12 | completed_models.append(current_design) 13 | def show_completed_models(completed_models): 14 | print("\nThe following models have been printed:") 15 | for completed_model in completed_models: 16 | print(completed_model) 17 | -------------------------------------------------------------------------------- /python_9th_answers/9th_1-15.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Dec 3 14:57:24 2017 4 | 5 | @author: LSayhi 6 | """ 7 | #homework for python 9th 1-15 8 | #9-1 9 | ''' 10 | class Restaurant(): 11 | """餐馆类""" 12 | def __init__(self,restaurant_name,cuisine_type): 13 | self.restaurant_name=restaurant_name 14 | self.cuisine_type=cuisine_type 15 | def describe_restaurant(self): 16 | print("The restaurant's name is "+self.restaurant_name+ 17 | "\nThe restaurant's type is "+self.cuisine_type) 18 | def open_restaurant(self): 19 | print('The restaurant is now opening') 20 | restaurant= Restaurant('鱼米之乡','川菜系') 21 | restaurant.describe_restaurant() 22 | restaurant.open_restaurant() 23 | ''' 24 | #9-2 25 | ''' 26 | class Restaurant(): 27 | def __init__(self,restaurant_name,cuisine_type): 28 | self.restaurant_name=restaurant_name 29 | self.cuisine_type=cuisine_type 30 | def describe_restaurant(self): 31 | print("The restaurant's name is "+self.restaurant_name+ 32 | "\nThe restaurant's type is "+self.cuisine_type) 33 | def open_restaurant(self): 34 | print('The restaurant is now opening') 35 | restaurant1= Restaurant('鱼米之乡','川菜系') 36 | restaurant1.describe_restaurant() 37 | restaurant2= Restaurant('赣南食府','江浙系') 38 | restaurant2.describe_restaurant() 39 | restaurant3= Restaurant('心灵鸡排','fast food') 40 | restaurant3.describe_restaurant() 41 | ''' 42 | #9-3 43 | ''' 44 | class User(): 45 | def __init__(self,first_name,last_name,**user_info): 46 | self.first_name=first_name 47 | self.last_name =last_name 48 | self.user_info =user_info 49 | def descibe_user(self): 50 | print('The user is '+self.first_name+self.last_name) 51 | print('following are his/her information:') 52 | print(self.user_info) 53 | def greet_user(self): 54 | print('weclome,'+self.first_name+self.last_name) 55 | user1=User('liu','jiangfan',age='21',location='Beijing') 56 | user1.descibe_user() 57 | user1.greet_user() 58 | user2=User('jiang','zeming',age='92',location='Beijing') 59 | user2.descibe_user() 60 | user2.greet_user() 61 | ''' 62 | #9-4 63 | ''' 64 | class Restaurant(): 65 | def __init__(self,restaurant_name,cuisine_type): 66 | self.restaurant_name=restaurant_name 67 | self.cuisine_type=cuisine_type 68 | self.number_served=0 69 | def describe_restaurant(self): 70 | print("The restaurant's name is "+self.restaurant_name+ 71 | "\nThe restaurant's type is "+self.cuisine_type) 72 | def open_restaurant(self): 73 | print('The restaurant is now opening') 74 | def set_number_served(self,latest_number): 75 | self.number_served=latest_number 76 | def increment_number_served(self,plus_number): 77 | self.number_served+=plus_number 78 | restaurant= Restaurant('鱼米之乡','川菜系') 79 | restaurant.describe_restaurant() 80 | restaurant.open_restaurant() 81 | print(restaurant.number_served) 82 | restaurant.set_number_served(200) 83 | print(restaurant.number_served) 84 | restaurant.increment_number_served(36) 85 | print(restaurant.number_served) 86 | ''' 87 | #9-5 88 | ''' 89 | class User(): 90 | def __init__(self,first_name,last_name,**user_info): 91 | self.first_name=first_name 92 | self.last_name =last_name 93 | self.user_info =user_info 94 | self.login_attempts=0 95 | def descibe_user(self): 96 | print('The user is '+self.first_name+self.last_name) 97 | print('following are his/her information:') 98 | print(self.user_info) 99 | def greet_user(self): 100 | print('weclome,'+self.first_name+self.last_name) 101 | def increment_login_attempts(self): 102 | self.login_attempts+=1 103 | def reset_login_attempts(self): 104 | self.login_attempts=0 105 | user1=User('liu','jiangfan',age='21',location='Beijing') 106 | user1.descibe_user() 107 | print(user1.login_attempts) 108 | user1.increment_login_attempts() 109 | user1.increment_login_attempts() 110 | print(user1.login_attempts) 111 | user1.reset_login_attempts() 112 | print(user1.login_attempts) 113 | ''' 114 | #9-6 115 | ''' 116 | class Restaurant(): 117 | """餐馆类""" 118 | def __init__(self,restaurant_name,cuisine_type): 119 | self.restaurant_name=restaurant_name 120 | self.cuisine_type=cuisine_type 121 | def describe_restaurant(self): 122 | print("The restaurant's name is "+self.restaurant_name+ 123 | "\nThe restaurant's type is "+self.cuisine_type) 124 | def open_restaurant(self): 125 | print('The restaurant is now opening') 126 | class Icecreamstand(Restaurant): 127 | """冰淇凌店类,继承Restaurant类""" 128 | def __init__(self,restaurant_name,cuisine_type): 129 | super().__init__(restaurant_name,cuisine_type) 130 | self.flavors=['a','b','c'] 131 | def describe_icecreams(self): 132 | print('following are flavor types we have: ') 133 | for flavor in self.flavors: 134 | print(flavor.strip()) 135 | icecream=Icecreamstand('KFC','fast food') 136 | icecream.describe_restaurant() 137 | icecream.describe_icecreams() 138 | ''' 139 | #9-7 140 | ''' 141 | class User(): 142 | """用户信息""" 143 | def __init__(self,first_name,last_name,**user_info): 144 | self.first_name=first_name 145 | self.last_name =last_name 146 | self.user_info =user_info 147 | self.login_attempts=0 148 | def descibe_user(self): 149 | print('The user is '+self.first_name+self.last_name) 150 | print('following are his/her information:') 151 | print(self.user_info) 152 | def greet_user(self): 153 | print('weclome,'+self.first_name+self.last_name) 154 | def increment_login_attempts(self): 155 | self.login_attempts+=1 156 | def reset_login_attempts(self): 157 | self.login_attempts=0 158 | class Admin(User): 159 | """管理员类,继承user类""" 160 | def __init__(self,first_name,last_name,**user_info): 161 | super().__init__(first_name,last_name,**user_info) 162 | self.privileges=['can add post','can delete post','can ban user'] 163 | def show_privileges(self): 164 | print('following are privileges admins have: ') 165 | for privilege in self.privileges: 166 | print(privilege.strip()) 167 | adminuser=Admin('Liu','Jiangfan',level='admin') 168 | adminuser.descibe_user() 169 | adminuser.show_privileges() 170 | ''' 171 | #9-8 172 | ''' 173 | class Privilege(): 174 | """特权类""" 175 | def __init__(self): 176 | self.privileges=['can add post','can delete post','can ban user'] 177 | def show_privileges(self): 178 | print('following are privileges admins have: ') 179 | for privilege in self.privileges: 180 | print(privilege.strip()) 181 | class User(): 182 | """用户信息""" 183 | def __init__(self,first_name,last_name,**user_info): 184 | self.first_name=first_name 185 | self.last_name =last_name 186 | self.user_info =user_info 187 | self.login_attempts=0 188 | def descibe_user(self): 189 | print('The user is '+self.first_name+self.last_name) 190 | print('following are his/her information:') 191 | print(self.user_info) 192 | def greet_user(self): 193 | print('weclome,'+self.first_name+self.last_name) 194 | def increment_login_attempts(self): 195 | self.login_attempts+=1 196 | def reset_login_attempts(self): 197 | self.login_attempts=0 198 | class Admin(User): 199 | """管理员类,继承user类""" 200 | def __init__(self,first_name,last_name,**user_info): 201 | super().__init__(first_name,last_name,**user_info) 202 | self.privileges=Privilege() 203 | adminuser=Admin('Liu','Jiangfan',level='admin') 204 | adminuser.descibe_user() 205 | adminuser.privileges.show_privileges() 206 | ''' 207 | #9-9 208 | ''' 209 | class Car(): 210 | """汽车类""" 211 | def __init__(self,make,model,year): 212 | self.make=make 213 | self.model=model 214 | self.year=year 215 | self.odometer_reading=0 216 | def get_desciptive_name(self): 217 | long_name=str(self.year)+' '+self.make+' '+self.model 218 | return long_name 219 | def read_read_odometer(self): 220 | print("This car has "+str(self.odometer_reading)+" miles on it") 221 | def update_odometer(self,mileage): 222 | if mileage >= self.odometer_reading: 223 | self.odometer_reading=mileage 224 | else: 225 | print("You can't roll back an odometer!") 226 | def increment_odometer(self,miles): 227 | self.odometer_reading+=miles 228 | class Battery(): 229 | """电池类""" 230 | def __init__(self,battery_size=70): 231 | self.battery_size=battery_size 232 | def describe_battery(self): 233 | print("This car has a "+str(self.battery_size)+"-kwh battery") 234 | def get_range(self): 235 | if self.battery_size==70: 236 | ranges=240 237 | elif self.battery_size==85: 238 | ranges=270 239 | msg="This car can go approximately "+str(ranges) 240 | msg+=" miles on a full charge." 241 | print(msg) 242 | def upgrade_battery(self): 243 | if self.battery_size !=85: 244 | self.battery_size=85 245 | class ElectricCar(Car): 246 | """电动汽车,继承Car""" 247 | def __init__(self,make,model,year): 248 | super().__init__(make,model,year) 249 | self.battery=Battery() 250 | my_tesla=ElectricCar('Tesla','model s',2016) 251 | print(my_tesla.get_desciptive_name()) 252 | my_tesla.battery.describe_battery() 253 | my_tesla.battery.get_range() 254 | my_tesla.battery.upgrade_battery() 255 | my_tesla.battery.get_range() 256 | ''' 257 | #9-10 258 | ''' 259 | """从restaurant.py中导入Restaurant类""" 260 | from restaurant import Restaurant 261 | restaurant= Restaurant('鱼米之乡','川菜系') 262 | restaurant.describe_restaurant() 263 | restaurant.open_restaurant() 264 | print(restaurant.number_served) 265 | restaurant.set_number_served(200) 266 | print(restaurant.number_served) 267 | restaurant.increment_number_served(36) 268 | print(restaurant.number_served) 269 | ''' 270 | #9-11 271 | ''' 272 | """从user.py模块中导入三个类""" 273 | from user import Privilege,User,Admin 274 | adminuser=Admin('Liu','Jiangfan',level='admin') 275 | adminuser.descibe_user() 276 | adminuser.privileges.show_privileges() 277 | ''' 278 | #9-12 279 | ''' 280 | """从两个模块中导入三个类""" 281 | from user_only import User 282 | from admin_privileges import Admin,Privilege 283 | adminuser=Admin('Liu','Jiangfan',level='admin') 284 | adminuser.descibe_user() 285 | adminuser.privileges.show_privileges() 286 | ''' 287 | #9-13 288 | ''' 289 | from collections import OrderedDict 290 | words=OrderedDict() 291 | words['while']="return 'true' if expresstion in 'while' is satisified" 292 | words['elif '] ='use after if' 293 | words['else '] ='use after if or elif' 294 | words['for '] ='use to express loop' 295 | words['# '] ='use to express notes' 296 | for name,meaning in words.items(): 297 | print(' '+name+':\t'+meaning) 298 | ''' 299 | #9-14 300 | ''' 301 | from random import randint 302 | class Die(): 303 | """骰子类""" 304 | def __init__(self,sides=6): 305 | self.sides=sides 306 | def roll_die(self): 307 | print(randint(1,self.sides)) 308 | x=Die(6) 309 | for i in range(10): 310 | x.roll_die() 311 | y=Die(10) 312 | for i in range(10): 313 | y.roll_die() 314 | z=Die(20) 315 | for i in range(10): 316 | z.roll_die() 317 | ''' 318 | #9-15 319 | """...""" -------------------------------------------------------------------------------- /python_9th_answers/__pycache__/Restaurant.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_9th_answers/__pycache__/Restaurant.cpython-36.pyc -------------------------------------------------------------------------------- /python_9th_answers/__pycache__/admin_privileges.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_9th_answers/__pycache__/admin_privileges.cpython-36.pyc -------------------------------------------------------------------------------- /python_9th_answers/__pycache__/user.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_9th_answers/__pycache__/user.cpython-36.pyc -------------------------------------------------------------------------------- /python_9th_answers/__pycache__/user_only.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/python_9th_answers/__pycache__/user_only.cpython-36.pyc -------------------------------------------------------------------------------- /python_9th_answers/admin_privileges.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Dec 5 22:25:02 2017 4 | 5 | @author: LSayhi 6 | """ 7 | """特权类和管理员类""" 8 | from user_only import User 9 | class Privilege(): 10 | """特权类""" 11 | def __init__(self): 12 | self.privileges=['can add post','can delete post','can ban user'] 13 | def show_privileges(self): 14 | print('following are privileges admins have: ') 15 | for privilege in self.privileges: 16 | print(privilege.strip()) 17 | class Admin(User): 18 | """管理员类,继承user类""" 19 | def __init__(self,first_name,last_name,**user_info): 20 | super().__init__(first_name,last_name,**user_info) 21 | self.privileges=Privilege() -------------------------------------------------------------------------------- /python_9th_answers/restaurant.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Dec 5 22:09:32 2017 4 | 5 | @author: LSayhi 6 | """ 7 | """一个可用于表示餐馆的类""" 8 | class Restaurant(): 9 | def __init__(self,restaurant_name,cuisine_type): 10 | self.restaurant_name=restaurant_name 11 | self.cuisine_type=cuisine_type 12 | self.number_served=0 13 | def describe_restaurant(self): 14 | print("The restaurant's name is "+self.restaurant_name+ 15 | "\nThe restaurant's type is "+self.cuisine_type) 16 | def open_restaurant(self): 17 | print('The restaurant is now opening') 18 | def set_number_served(self,latest_number): 19 | self.number_served=latest_number 20 | def increment_number_served(self,plus_number): 21 | self.number_served+=plus_number 22 | -------------------------------------------------------------------------------- /python_9th_answers/user.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Dec 5 22:18:26 2017 4 | 5 | @author: LSayhi 6 | """ 7 | """定义用户类,管理员类,特权类""" 8 | class Privilege(): 9 | """特权类""" 10 | def __init__(self): 11 | self.privileges=['can add post','can delete post','can ban user'] 12 | def show_privileges(self): 13 | print('following are privileges admins have: ') 14 | for privilege in self.privileges: 15 | print(privilege.strip()) 16 | class User(): 17 | """用户信息""" 18 | def __init__(self,first_name,last_name,**user_info): 19 | self.first_name=first_name 20 | self.last_name =last_name 21 | self.user_info =user_info 22 | self.login_attempts=0 23 | def descibe_user(self): 24 | print('The user is '+self.first_name+self.last_name) 25 | print('following are his/her information:') 26 | print(self.user_info) 27 | def greet_user(self): 28 | print('weclome,'+self.first_name+self.last_name) 29 | def increment_login_attempts(self): 30 | self.login_attempts+=1 31 | def reset_login_attempts(self): 32 | self.login_attempts=0 33 | class Admin(User): 34 | """管理员类,继承user类""" 35 | def __init__(self,first_name,last_name,**user_info): 36 | super().__init__(first_name,last_name,**user_info) 37 | self.privileges=Privilege() -------------------------------------------------------------------------------- /python_9th_answers/user_only.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Dec 5 22:23:22 2017 4 | 5 | @author: LSayhi 6 | """ 7 | """单独的User类,useronly""" 8 | class User(): 9 | """用户信息""" 10 | def __init__(self,first_name,last_name,**user_info): 11 | self.first_name=first_name 12 | self.last_name =last_name 13 | self.user_info =user_info 14 | self.login_attempts=0 15 | def descibe_user(self): 16 | print('The user is '+self.first_name+self.last_name) 17 | print('following are his/her information:') 18 | print(self.user_info) 19 | def greet_user(self): 20 | print('weclome,'+self.first_name+self.last_name) 21 | def increment_login_attempts(self): 22 | self.login_attempts+=1 23 | def reset_login_attempts(self): 24 | self.login_attempts=0 -------------------------------------------------------------------------------- /test.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LSayhi/Python/44df36ffcbf1d4daced980d12300e7efa2c4f8e2/test.txt --------------------------------------------------------------------------------