So, I've got an algorithm whereby I take a character, take its character code, increase that code by a variable, and then print that new character. However, I'd also like it to work for characters not in the default ASCII table. Currently it's not printing 'special' characters like €
(for example). How can I make it print certain special characters?
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def generateKey(name):
i = 0
result = ""
for char in name:
newOrd = ord(char) + i
newChar = chr(newOrd)
print(newChar)
result += newChar
i += 1
print("Serial key for name: ", result)
generateKey(input("Enter name: "))
Whenever I give an input that forces special characters (like |||||
), it works fine for the first four characters (including DEL
where it gives the transparent rectangle icon), but the fifth character (meant to be €
) is also an error char, which is not what I want. How can I fix this?
Here's the output from |||||
:
Enter name: |||||
|
}
~
Serial key for name: |}~
But the last char should be €
, not a blank. (BTW the fourth char, DEL, becomes a transparent rectangle when I copy it into Windows)
In the default encoding (utf-8), chr(128)
is not the euro symbol. It's a control character. See this Unicode table. So indeed it should be blank, not €
.
You can verify the default encoding with sys.getdefaultencoding()
.
If you want to reinterpret chr(128)
as the euro symbol, you should use the windows-1252
encoding. There, it is indeed the euro symbol. (Different encodings disagree on how to represent values beyond ASCII's 0–127.)