Python 在函数中包装代码

Python 在函数中包装代码,python,function,python-2.7,Python,Function,Python 2.7,写一个函数 您好,姓名您今年是当前年龄 到目前为止,我有: from datetime import date p = current_year = date.today().year name = raw_input("enter your name") y = raw_input("enter ur month of birth") z = raw_input("enter your year of birth") current_age =(int(p)-int(z)) print

写一个函数 您好,姓名您今年是当前年龄

到目前为止,我有:

from datetime import date
p = current_year = date.today().year

name = raw_input("enter your name")
y = raw_input("enter ur month of birth")
z = raw_input("enter your year of birth")

current_age =(int(p)-int(z))

print current_age
print("Hi,", name, " you are" , current_age, " years old this year!"
  "Here's a bouquet of flowers for you!")
这段代码可以工作,但我正在尝试将其转换为函数。有人能帮忙吗

from datetime import date
p = current_year = date.today().year

def bday_wish(name,y,z):
    current_age =(int(p)-int(z))

print current_age
print("Hi,", name, " you are" , current_age, " this year!")

定义一个名为“显示生日愿望”的函数。将要运行的代码放入其中,需要时调用函数

from datetime import date

def display_birthday_wishes(): #define the function, no arguments passed in
    p = current_year = date.today().year

    name = raw_input("Enter your name: ")
    y = raw_input("Enter the month you were born in: ")
    z = raw_input("Enter your year of birth ")

    current_age =(int(p)-int(z))

    print current_age
    print"Hi,", name, "you are" , current_age, "years old this year! Here's a bouquet of flowers for you!"

display_birthday_wishes() #call the function

有关定义函数的详细信息

如果要将个人信息作为参数,可以使用以下方法:

from datetime import date

def display_birthday_wishes(name, month, year):
    p = current_year = date.today().year
    current_age = (int(p)-int(year))

    return "Hi, " + name + ". You are " + str(current_age) +\
    " years old this year! Here's a bouquet of flowers for you!"

print display_birthday_wishes("David", 4, 1998)
from datetime import date

def display_birthday_wishes():
    name = raw_input("enter your name")

    current_year = current_year = date.today().year
    year_born = raw_input("enter your year of birth")

    current_age = (int(current_year)-int(year_born))

    month_born = raw_input("enter your month of birth")


    return "Hi, " + name + ". You are " + str(current_age) +\
    " years old this year! Here's a bouquet of flowers for you!"

print display_birthday_wishes()
但是,如果仍要使用
原始输入
获取信息,可以使用以下方法:

from datetime import date

def display_birthday_wishes(name, month, year):
    p = current_year = date.today().year
    current_age = (int(p)-int(year))

    return "Hi, " + name + ". You are " + str(current_age) +\
    " years old this year! Here's a bouquet of flowers for you!"

print display_birthday_wishes("David", 4, 1998)
from datetime import date

def display_birthday_wishes():
    name = raw_input("enter your name")

    current_year = current_year = date.today().year
    year_born = raw_input("enter your year of birth")

    current_age = (int(current_year)-int(year_born))

    month_born = raw_input("enter your month of birth")


    return "Hi, " + name + ". You are " + str(current_age) +\
    " years old this year! Here's a bouquet of flowers for you!"

print display_birthday_wishes()

使用
def
定义函数,并使用
name
y
z
作为参数。注释:您正在打印此人今年的年龄。我想这可能是你想要的,因此有了“今年岁”这段文字。不过,我不会称之为“当前年龄”。如果你不打算使用这些信息,为什么还要问一个月?除此之外,为什么不询问他们的确切出生日期呢?因为月份和年份仍然没有足够的信息来判断一个人的年龄。