So I wrote a program that converted alphabets from a sentence to numbers. To understand whether the number was in the same word or another I made a distinction with "-" and "-". If a word is in the same line the numbers are separated by a "-" and if they are in another word they are separated by a "-". Like hi hi => 8-5-*-8-5-.
Now I was writing a program to do the opposite i.e convert numbers to alphabets using a dictionary. I wrote the dictionary like this -
Dictionary = {"26" : "z","25" : "y","24" : "x","23" : "w","22" : "v","21" : "u","20" : "t","19" : "s","18" : "r","17" : "q","16" : "p","15" : "o","14" : "n","13" : "m","12" : "l","11" : "k","10" :"j","1" : "a","2" : "b","3" : "c","4" : "d","5" : "e","6" : "f","7" : "g","8" : "h","9" : "i","*" : " "}
user_input = input("Please Enter a message: ")
count = user_input.count("-")
individual_number = user_input.split("-")
for i in range(0 , count):
for number in individual_number[i]:
individual_number[i] = individual_number[i].replace(number, Dictionary[number])
print(individual_number[i])
Now it Works for numbers from 1 - 9 but for 10 - 26, it does not work. For Example -
8-9-*-2-6- => h
i
b
f
But the same is for this one as well -
8-9-*-26- => h
i
bf (This should have been "Z")
I don't understand why it does that. it should take the whole number instead of taking each number one by one. Please help.
Also, I want it to print the whole sentence in one line/string. But I can't get it to do it. Please help with that too. Thank you
CodePudding user response:
To convert numbers to words use this one.
Dictionary = {"26" : "z","25" : "y","24" : "x","23" : "w","22" : "v","21" : "u","20" : "t","19" : "s","18" : "r","17" : "q","16" : "p","15" : "o","14" : "n","13" : "m","12" : "l","11" : "k","10" :"j","1" : "a","2" : "b","3" : "c","4" : "d","5" : "e","6" : "f","7" : "g","8" : "h","9" : "i","*" : " "}
user_input = input("Please Enter a message: ")
user_input = user_input.split('-')
output = ''
for a in user_input:
try:
output =Dictionary[a]
except KeyError:
output =''
print(output)
And for words to numbers use this one.
Dictionary = {"26" : "z","25" : "y","24" : "x","23" : "w","22" : "v","21" : "u","20" : "t","19" : "s","18" : "r","17" : "q","16" : "p","15" : "o","14" : "n","13" : "m","12" : "l","11" : "k","10" :"j","1" : "a","2" : "b","3" : "c","4" : "d","5" : "e","6" : "f","7" : "g","8" : "h","9" : "i","*" : " "}
Dictionary_ = {value:key for key,value in Dictionary.items()}
user_input = input("Please Enter a message: ")
output = ''
for a in user_input:
try:
output =Dictionary_[a]
output ='-'
except KeyError:
output =''
output = output[:-1]
print(output)
CodePudding user response:
I think you're over-complicating things. Why the nested loop?
This seems to do what you want:
Dictionary = {"26" : "z","25" : "y","24" : "x","23" : "w","22" : "v","21" : "u","20" : "t","19" : "s","18" : "r","17" : "q","16" : "p","15" : "o","14" : "n","13" : "m","12" : "l","11" : "k","10" :"j","1" : "a","2" : "b","3" : "c","4" : "d","5" : "e","6" : "f","7" : "g","8" : "h","9" : "i","*" : " "}
user_input = input("Please Enter a message: ")
individual_number = user_input.split("-")
for number in individual_number:
print(Dictionary[number],end='')
CodePudding user response:
Rather than hard-coding the dictionary, build it using the string module and a dictionary comprehension.
Split your string on hyphen and try lookup in your dictionary. Use get() in case there's something unexpected in the string
Dictionary = {str(i 1): chr(ord('a') i) for i in range(26)}
val = '8-9-*-26-'
print(''.join(Dictionary.get(v, ' ') for v in val.split('-')))
Output:
hi z
Note:
ANY token in the split list that is not in the dictionary will result in ' '