2021年5月7日星期五

How do I use ASCII (and not Unicode) in Python?

I'm trying to make a Caesar cipher encryptor/decryptor in Python, by getting the ASCII index of a letter and adding/subtracting from it. However, it seems to be using Unicode instead because I get letters with accents instead of just English alphabet letters. For example, if I enter the word 'weasel' and encrypt it by 2, what I get is 'yÞĿƲȗʃ'. How do I use ASCII?

Here is my code:

    def manipulate(text, key, e_or_d):          ciphertext = ''          for i in text:              asciiCode = ord(i)              if e_or_d == 'e':                  key = ord(i) + key              elif e_or_d == 'd':                  key = ord(i) - key              newChar = chr(key)              ciphertext += newChar          if e_or_d == 'e':                  print('Here is your encrypted text: ')          elif e_or_d == 'd':              print('Here is your decrypted text: ')          print(ciphertext)            text = input('Enter text: ')      e_or_d = input('Enter \'e\' for encryption or \'d\' for decryption: ')      key = int(input('Enter encryption key: '))      manipulate(text, key, e_or_d)  

I used chr and ord here to turn an index into a character and identify the index of a character, respectively.

Sorry if this is incoherent and disorganised.

https://stackoverflow.com/questions/67433306/how-do-i-use-ascii-and-not-unicode-in-python May 07, 2021 at 06:32PM

没有评论:

发表评论