Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cs50
- import sys
- def main():
- # make sure the user gives the right parameters
- if len(sys.argv) != 2:
- print("python caesar.py key")
- exit(1)
- # make sure the input are valid alabetical characters
- if sys.argv[1].isalpha() == False:
- print("plase provide valid input")
- exit(2)
- else:
- key = sys.argv[1]
- # prompt the user for text to be ciphered
- print("plaintext: ", end="")
- plaintext = cs50.get_string()
- print("ciphertext: ", end="")
- # prepare to do the ciphering
- key_index = 0
- key_len = len(sys.argv[1])
- ciphertext = []
- # loop and cipher
- for i in plaintext:
- if i.isalpha():
- cipher_key = ord(key[key_index % key_len].lower()) - 97
- ciphertext.append(cipher_this(i, cipher_key))
- cipher_key += 1
- else:
- ciphertext.append(i)
- # print the final solution
- print("".join(ciphertext))
- exit(0)
- def cipher_this(char, key):
- if char.isupper():
- return chr(((ord(char) - 65 + key) % 26) + 65)
- elif char.islower():
- return chr(((ord(char) - 97 + key) % 26) + 97)
- if __name__=="__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement