P29 函数
Sometime we copy and paste our code
Little trick i using to try and debug my code, figure our what was taking a lot of time.
first_name = 'Susan'
print('task completed')
print(datetime.datetime.now())
print()
for x in range(0,10):
print(x)
print('task completed')
print(datetime.datetime.now())
print()
Use functions instead of repeating code
import datetime
#Print the current time
def print_time():
print('task completed')
print(datetime.datetime.now())
print()
first_name = 'Susan'
print_time()
for x in range (0,10):
print(x)
print_time()
In the datetime library call the datetime object call the now function of that datetime object. -----It's pretty clunky
By moving the code to a function, you reduce rework and the chance of introducing bugs when you change the code you hand copied.
#Import the datetime class from datetime library
from datetime import datetime
#Print the current time
def print_time():
print('task completed')
#Now I don't need the extra datetime prefix
print(datetime.now())
print_time()
What if I want a different message displayed?
from datetime import datetime
#print timestamps to see how long sections of code
#take to run
first_name = 'Susan'
print('first name assigned')
print(datetime.now())
print()
for x in range(0,10):
print(x)
print('loop completed')
print(datetime.now())
print()
Pass the task name as a parameter
from datetime import datetime
# Print the current time and task name
def print_time(task_name):#make up you own parameter names
print(task_name)
print(datetime.now())
first_name = 'Susan'
print_time('first name assigned')
for x in range(0,10):
print(x)
print_time('loop completed')
parameters allow you a lot more flexibility when you're creating your functions.
Here's another example where the code looks different but we are doing the same logic over and over
first_name = input('Enter your first name: ')
fitst_name_initial = first_name[0:1]
last_name = input('Enter your last name: ')
last_name_initial = last_name[0:1]
print('Your initials are: ' + fitst_name_initial + last_name_initial)
I can still use a function, but this time my function returens a value
def get_initial(name):
initial = name[0:1]
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name)
last_name = input('Enter your last name: ')
last_name_initial = get_initial(last_name)
print('Your initials are: ' + first_name_initial + last_name_initial)
If you need to change something you only have to change it in one place
def get_initial(name):
initial = name[0:1].upper() #change
return initial
first_name = input('Enter your first name: ')
first_name_initial = get_initial(first_name)
last_name = input('Enter your last name: ')
last_name_initial = get_initial(last_name)
print('Your initials are: ' + first_name_initial + last_name_initial)
Functions that return values allow clever code, but you might trade readability for less code
def get_initial(name):
initial = name[0:1].upper()
return initial
first_name = input('Enter your first name: ')
last_name = input('Enter your last name: ')
print('Your initials are: '+get_initial(first_name)+get_initial(last_name))
Because they return values you can call them in different ways, You can either call them and take the value that you receive and return it into a variable or you can actually call it directly inside statements.
Functions make you code more readable and easier to maintain
Always add comments to explain the purpose of your functions
Functions must be declared before the line of code where the function is called
P30 实操函数
import datetime
first_name = 'Susan'
print('task completed')
print(datetime.datetime.now())
print()
for x in range(0,10):
print(x)
print('task completed')
print(datetime.datetime.now())
print()
(又学到12点多了呜呜呜QAQ,没到天亮还算14号吧!)
如何把datetime.datetime.now()简化
把import datetime改为 form datetime import datetime 后面就只用写datetime.now了
·Now waht if I decide to display custom messages如果想显示自定义信息怎么办,So a different message depending on what task just completed执行不同的任务有不同的信息。
给定义的函数加参数 def print_time(task_name) 并在后面调用的时候设置对应的值print_time('printed first name') print_time('completed for loop')
import datetime
def print_time(task_name):
print(task_name)
print(datetime.datetime.now())
print()
first_name = input('your name: ')
print(first_name)
print_time('名字')
for x in range(0,10):
print(x)
print_time('数字')
效果如下:
复杂一点的:
#This function will take a name and return the first letter of the name
#first letter of the name
def get_initial(name):
initial = name[0:1].upper()
return initial
#Ask for someone's name and return the initials
first_name = input('Enter your first name: ')
middle_name = input('Enter your middle name: ')
last_name = input('Enter your last name: ')
print ('Your initials are: ' + get_initial(first_name) + get_initial(middle_name) + get_initial(last_name))
今天搞太晚了,大家不要学我熬夜