I need to convert a string of numbers into a string of letters to form words
Example:
if the input to your program is
1920012532082114071825463219200125320615151209190846 it should return
stay hungry. stay foolish.
I have this so far:
def number ():
output = []
input = raw_input ("please enter a string of numbers: ")
for number in input:
if number <= 26:
character = chr(int(number+96))
output.append(character)
else:
character = chr(int(number))
output.append(character)
print output
I need it to somehow determine that every two numbers equals one letter.
I have a program that does the reverse of this, outputs numbers when given letter. this is what that looks like:
def word ():
output = []
input = raw_input("please enter a string of lowercase characters: ")
for character in input:
number = ord(character) - 96
if number > 0:
if number <= 9:
output.append('0' + str(number))
else:
output.append(str(number))
else:
output.append(str(number + 96))
print ''.join(output)
Thanks for the help
You can split up the
strinto two-character chunks usingzip()and slicing with a step like this:I’d write the whole thing like this:
It’s more useful to have the logic separated from input and output, so you can use the function in other parts of a program. It’s also a good idea to avoid repeating the use of names like
numberorinput(which is a built-in).