defbuild_person(first_name,last_name,age = ''): """返回一个字典,其中包含有关一个人的信息""" person = {'first':first_name,'last':last_name} if age: person['age'] = age pass
while unconfirmed_users: """处理用户认证操作""" current_user = unconfirmed_users.pop() print("verifying User:"+current_user) confirmed_users.append(current_user)
"""打印认证用户""" print("\nThe following users have been confirmed: ") for user in confirmed_users: print(user.title()) 得到: verifying User:three verifying User:two verifying User:one
The following users have been confirmed: Three Two One
The following users have been confirmed: Third Second First
展示原始数据: First Second Third
5.传递任意数量的实参。
有的时候我们不知道函数需要接受多少个实参,python允许函数从调用语句中收集任意数量的实参。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
“”“这里*toppons指定了一个空元组,将收到的所有值都封装在这个这元组中。”“” defmake_pizza(*toppons): """打印顾客点的所有配料""" print("\nMaking a pizza with the following toppings") for top in toppons: print("- "+top.title())
def make_pizza(size,*toppons): """打印顾客点的所有配料""" print("\nMaking a " + str(size) +"-inch pizza with the following toppings") for top in toppons: print("- "+top.title())
defmake_pizza(size,*toppons): """打印顾客点的所有配料""" print("\nMaking a " + str(size) +"-inch pizza with the following toppings") for top in toppons: print("- "+top.title())
defmake_pizza(size,*toppons): """打印顾客点的所有配料""" print("\nMaking a " + str(size) +"-inch pizza with the following toppings") for top in toppons: print("- "+top.title())
defmake_KFC(size,*toppons): """打印顾客点的所有配料""" print("\nMaking a " + str(size) +"-inch KFC with the following toppings") for top in toppons: print("- "+top.title())
defupdate_odometer(self, mileage): if mileage >= self.odometer_reading: self.odometer_reading = mileage else: print("you can't roll back an odometer!")
print("Give me two numbers, and I'll divide them.") print("Enter 'q' to quit. ") whileTrue: first_number = input("\nFirst number: ") if first_number == 'q': break;
second_number = input("\nSecond number: ") if second_number == 'q': break;
answer = int(first_number)/int(second_number) print(answer) 得到: liukingdeMacBook-Pro:desktop liuking$ python3 input.py Give me two numbers, and I'll divide them. Enter 'q' to quit. First number: 4 Second number: 2 2.0 First number: 4 Second number: g Traceback (most recent call last): File "input.py", line 244, in <module> answer = int(first_number)/int(second_number) ValueError: invalid literal for int() with base 10: 'g' liukingdeMacBook-Pro:desktop liuking$
print("Give me two numbers, and I'll divide them.") print("Enter 'q' to quit. ") whileTrue: first_number = input("\nFirst number: ") if first_number == 'q': break;
second_number = input("\nSecond number: ") if second_number == 'q': break;
得到: liukingdeMacBook-Pro:desktop liuking$ python3 input.py Give me two numbers, and I'll divide them. Enter 'q' to quit. First number: 5 Second number: 3 1.6666666666666667 First number: 5 Second number: 0 you can't divide by 0!
def get_formatted_name(first,last,middle = ''): """Generate a neatly formatted full name.""" if middle: full_name = first + ' ' + middle + ' ' + last else: full_name = first + ' ' + last
return full_name.title() 得到: . ---------------------------------------------------------------------- Ran 1 test in 0.000s
defshow_results(self): """显示收集到的所有答案""" print("Survey Results:") for response in self.responses: print('- '+response) ------------------------------------------------------------------------------------------ from survey import AnonymousSurvey
#定义一人问题,并创建一个表示调查的AnonymousSurvey对象 question = "What language did you first learn to speak?" my_survey = AnonymousSurvey(question)
my_survey.show_question() print("Enter 'q' at any time to quit.\n") whileTrue: response = input("language: ") if response == 'q': break my_survey.store_response(response)
#显示调查结果: print("\nThank you to everyone who participated in the survey?") my_survey.show_results()
运行得到: 在终端运行得到: What language did you first learn to speak? Enter 'q' at any time to quit.
language: english language: chinese language: japanese language: q
Thank you to everyone who participated in the survey? Survey Results: - english - chinese - japanese
import unittest from survey import AnonymousSurvey
classTestAnonymousSurvey(unittest.TestCase): """docstring for ClassName""" deftest_store_single_response(self): question = "what language did you first learn to speak?" my_survey = AnonymousSurvey(question) my_survey.store_response('english')
self.assertIn('english',my_survey.responses)
unittest.main()
运行得到:
. ---------------------------------------------------------------------- Ran 1 test in0.000s
import unittest from survey import AnonymousSurvey
classTestAnonymousSurvey(unittest.TestCase): """docstring for ClassName""" deftest_store_single_response(self): question = "what language did you first learn to speak?" my_survey = AnonymousSurvey(question) my_survey.store_response('english')
self.assertIn('english',my_survey.responses)
deftest_store_three_responses(self): question = "what language did you first learn to speak?" my_survey = AnonymousSurvey(question) responses = ['english','chinese','japanese'] for response in responses: my_survey.store_response(response)
for response in responses: self.assertIn(response,my_survey.responses) unittest.main() 运行得到: .. ---------------------------------------------------------------------- Ran 2 tests in0.000s
import unittest from survey import AnonymousSurvey
classTestAnonymousSurvey(unittest.TestCase):
defsetUp(self): """创建一个调查对象和一组答案,供使用的测试方法使用。""" question = "What language did you first learn to speak?" self.my_survey = AnonymousSurvey(question) self.responses = ['chinese','english','japanese']
"""docstring for ClassName""" deftest_store_single_response(self): self.my_survey.store_response(self.responses[0]) self.assertIn(self.responses[0],self.my_survey.responses)
deftest_store_three_responses(self): for response in self.responses: self.my_survey.store_response(response)
for response in self.responses: self.assertIn(response,self.my_survey.responses) unittest.main()
运行得到: .. ---------------------------------------------------------------------- Ran 2 tests in0.000s