Quick actions

cmd+k|ctrl+k

Navigation

Languages

シーザー暗号

Snippet info

Language

Python

Visibility

public

Author

a9n1105

Created

2023-09-07T02:22:54.489906Z

Updated

2023-09-07T02:29:54.121198Z

import string

def cipher(a_string, key: int):
    uppercase = string.ascii_uppercase
    lowercase = string.ascii_lowercase
    encrypt = ""
    for c in a_string:
        if c in uppercase:  # iが大文字ならば
            new = (uppercase.index(c) + key) % 26
            encrypt += uppercase[new]
        if c in lowercase:
            new = (lowercase.index(c) + key) % 26
            encrypt += lowercase[new]
        if (c not in uppercase) and (c not in lowercase):
            encrypt += c
    return encrypt
    
a_str = "my name is Gaius Iulius Caesar."
print(cipher(a_str, 3))
INFO