file_name = raw_input('Name of the file you will use to encode the your data \n')
file_name = file_name+'.txt'
fp = open(file_name, 'r')
text = fp.read()

#create a string that is just the first letter of every word in the sentence then close file
output = ""
for i in text.split():
    output += i[0]
fp.close()

#Enter the information to be encoded
coordinates = raw_input('Write out the coordinates in word form and press <Enter>\n\n')

#convert string to lower case and shorten it to just 99 cariters
coordinates = coordinates .replace(' ', '')
lower_case = output.lower()
short_str = lower_case[0:99]

# initialize counter and cipher
count = 0
cipher = 0

#run through data and substitute index number of key file text for corresponding letter
#if next substitution cannot be found reset search to begining of coordinates string
for letter in coordinates:
    if short_str.find(letter, cipher, len(coordinates)) < 0:
        cipher = 0
    else:
        cipher += 1

    if cipher > 100:
        cipher = 0

    if count % 10 == 0:
        print
#print encoded string in blocks of ten digits each      
    cipher = short_str.find(letter, cipher)
    print cipher + 1,
    count += 1

The program works however, whenever, it comes accross a letter it can't find a substatution for in the text file it pints 0, and I'd like to have it print X or just and underscore. How can I do this?

Recommended Answers

All 5 Replies

Do give a little better idea of what this program does here's a example of what it displays:

Name of the file you will use to encode the your data
gettysburg_address

Write out the coordinates in word form and press <Enter>
thirty degrees twenty one point three hundred forty seven minutes north|

Which displays:

12 67 18 79 12 5 21 30 37 79
30 34 47 12 32 34 54 15 22 5
7 15 30 24 44 18 31 40 42 67
79 30 34 67 0 79 30 51 1 7
79 12 5 47 54 0 30 31 57 18
31 0 12 30 47 15 44 79 12 67

What'd like is:

12 67 18 79 12 5 21 30 37 79
30 34 47 12 32 34 54 15 22 5
7 15 30 24 44 18 31 40 42 67
79 30 34 67 _ 79 30 51 1 7
79 12 5 47 54 _ 30 31 57 18
31 _ 12 30 47 15 44 79 12 67

I can't really follow what you're doing but if you want to print a string with all the "0"s replaced with "_" you can "replace":
>>> "11110abc0".replace("0","_") '1111_abc_'

It would be something like

    cipher = short_str.find(letter, cipher)
    if cipher > -1:
       print cipher + 1,    
    else:
      print "_",

Ok, woooee that's got me on the right track it's still it formatting exactly the way I want it but, that is one step closer.

Ok the mytery is solved and the solution is:

#print encoded string in blocks of ten digits each      
    cipher = short_str.find(letter, cipher)
    if cipher > 0:
        print cipher + 1,
    else:
        print "_",

    count += 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.