【Python CheckiO 题解】Say Hi

CheckiO 是面向初学者和高级程序员的编码游戏，使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务，从而提高你的编码技能，本博客主要记录自己用 Python 在闯关时的做题思路和实现代码，同时也学习学习其他大神写的代码。

CheckiO 官网：https://checkio.org/

CheckiO 题解系列专栏：https://itrhx.blog.csdn.net/category_9536424.html

CheckiO 所有题解源代码：https://github.com/TRHX/Python-CheckiO-Exercise

题目描述

【Say Hi】：你的任务是编写一个根据给出的属性参数来介绍一个人的函数

【输入】：两个参数，一个字符串（str）和一个正整数（int）

【输出】：字符串（str）

【范例】

say_hi("Alex", 32) == "Hi. My name is Alex and I'm 32 years old"
say_hi("Frank", 68) == "Hi. My name is Frank and I'm 68 years old"


代码实现

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

def say_hi(name: str, age: int) -> str:
return "Hi. My name is " + name + " and I'm " + str(age) + " 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.')


大神解答

大神解答 NO.1

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


大神解答 NO.2

def say_hi(name: str, age: int) -> str:
return "Hi. My name is {0} and I'm {1} years old".format(name, age)


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客