lucky 的学习笔记(Python 函数)

lucky 的学习笔记(Python 函数)
0

这是一个带有 for 循环的指数运算函数示例:

def function(base_num, power_num):
    result = 1
    for index in range(power_num): #从 0 到 power_num-1
        result = result*base_num
    return result
print(function(2, 5))

在写这段代码的时候,发现 return 的位置不一样,输出的结果也不一样,对 for 循环理解不够:

def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter in "aeiou":
            translation = translation + "g"
        else:
            translation = translation + letter
    return translation

print(translate(input("Enter a phrase: ")))
2赞

well done!

可以看看菜鸟教程,有系统全面的讲解