I am a newbie, please forgive my ignorance. I tried researching and reading other articles and questions, but nothing seems to give a proper answer.
If the answer is simply no, please just tell me.
Basically, I'm working on a project for school, and I want to be able to take someone's input (i.e. their name) and convert that to an integer. For example, If I input Rob, I want the output to show 18 15 2 (R=18, o=15, b=2), or 35. Something along those lines. Or, simply convert Rob to 35 and use that as a comparison in an if-else statement.
If there is more than one command, or set-up involved (such as converting the letters of the alphabet to integers before asking for input from the user) I would appreciate anyone just pointing me in the right direction for more information. I'm very excited to learn, and I understand this may be more complicated than I originally thought.
The only options I've found so far were the "hash" command, which did not give me the results I was looking for. From what I understand, that gives a random number to a word, but I could be misinterpreting the information.
The other thing I tried was the "ord()" command. However, that seems to only convert one letter at a time.
I apologize if this has been answered elsewhere, but I wasn't able to find anything that worked. I greatly appreciate anyone who takes time to try and help!
I am using Spyder (Python 3.9) if that matters.
CodePudding user response:
The most efficient way of doing this would be to make a for loop over the string and then use sum function to add them all up.
string = "Rob"
ord_sum = sum(ord(c) for c in string)
print(ord_sum)
CodePudding user response:
Try with this:
name = input()
number = 0
for letter in name:
number = ord(letter)
Iterate through name, ord() will give you ASCII code for the char
CodePudding user response:
There's no "command" for this as far as I'm aware, but, you can use a custom-made encoder/decoder system. Here is one that I made a while ago in C#, but converted to Python:
def Encode(input):
chars = ["A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "Y", "y", "Z", "z", " "]
Index = 0
Result = ""
for char in input:
Index = chars.index(char) 10
Result = Result str(Index)
return Result
def Decode(input):
chars = ["A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "Y", "y", "Z", "z", " "]
Index = 0
Index2 = "0"
Check1 = 0
Check2 = 1
Result = ""
for char in input:
try:
Index2 = str(input[Check1]) str(input[Check2])
Index = int(Index2)
Result = Result chars[Index - 10]
Check1 = Check1 2
Check2 = Check2 2
except:
break
return Result
print(Encode("test"))
print(Decode("49194749"))
You can call the functions and they will do what it seems you want. The only thing is, for other characters besides normal letters and a space, you'll need to add it to the chars list on both functions.