Day 8 – 函数参数

Day 8 – 函数参数

温馨提示:本文最后更新于2024-12-12 10:59:49,某些文章具有时效性,若有错误或已失效,请在下方留言

有参函数

带有输入参数的函数即为有参函数,有参函数的基本语法

# 函数定义
def <function name>(param1, param2,...):
	# Do this with param1, param2,...

# 函数调用
<function name>(arg1, arg2, ...)

Parameter 与 Argument 的区别:
– Parameter: 形参,函数定义中的输出参数
– Argument:实参,函数调用中传入的参数

示例代码

def greet(name, greeting):
    print(f"{greeting} {name}")

greet("Jack", "Hello")
# Hello Jack

关键字参数

可以在调用函数时提供参数时使用关键字,以减少将哪个值分配给哪个输入参数的混淆。如下所示

def greet_with(name, location):
    print(f"Hello {name}")
    print(f"What is it like in {location}")

# 关键字参数
greet_with(location="London", name="Angela")

凯撒密码

凯撒密码(移位密码):是一种替换加密,明文中的所有字母都在字母表上向后或向前按照一个固定数目进行偏移后被替换成密文。
当偏移量为13位的时候,凯撒密码又叫回转密码(ROT13):明文加密得到密文,密文再加密就会得到明文(因为偏移量为13位,一共26个字母,加密两次就会回到明文了),在CTF中题目关键字眼会有回转、回旋、十三踢等字眼。

def caesar(original_text, shift_amount, encode_or_decode):
    output_text = ""
    if encode_or_decode == "decode":
        shift_amount *= -1

    for letter in original_text:

        if letter not in alphabet:
            output_text += letter
        else:
            shifted_position = alphabet.index(letter) + shift_amount
            shifted_position %= len(alphabet)
            output_text += alphabet[shifted_position]
    print(f"Here is the {encode_or_decode}d result: {output_text}")
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容