[python] say hi

写一个函数来介绍一个具有给定参数的人的属性。

输入:两个参数。字符串和正整数。

输出:字符串。

例:

say_hi “Alex” 32 == “嗨,我叫Alex,我32岁”   
say_hi “Frank” 68 == “嗨,我叫弗兰克,我68岁了” 

我的代码

# 1. on CheckiO your solution should be a function
# 2. the function should return the right answer, not print it.

def say_hi(name, age):
    """
        Hi!
    """
    # your code here
    return ("Hi. My name is {} and I'm {} years old".format(name,age))
    return "Hi. My name is Alex and I'm 32 years old"

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert say_hi("Alex", 32) == "Hi. My name is Alex and I'm 32 years old", "First"
    assert say_hi("Frank", 68) == "Hi. My name is Frank and I'm 68 years old", "Second"
    print('Done. Time to Check.')

大神代码

def say_hi(name, age):
    return f"Hi. My name is {name} and I'm {age} years old"

不同方法

say_hi = "Hi. My name is {} and I'm {} years old".format
return "Hi. My name is " + name + " and I'm " + str(age) + " years old"
t = Template("Hi. My name is $name and I'm $age years old")
return t.substitute(name=name, age=age)




你可能感兴趣的:(python)