954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I need help writing to a file (encoding program)

I am trying to do this simple encoding program for class. It is supposed to encode it using the ASCII chart and some slight modding of the number. Then it should be written to a file 'encryptedmessage.txt' for later reading by a decoder later. My issue is I dont know how to add newMessage so I can write it to file. I can't use the for loop in the output file. Any help would be appreciated. i tried searching but everything I found was actually more complex then what we are into as of now. Thank you for taking the time to read this.

def main():

    #declare and initialize variables
    #string message
    newMessage = message = ""

    #Intro
    print("+++++++++++++++++++++++++++++++")
    print("Welcome to the encoder program!")
    print("+++++++++++++++++++++++++++++++")

    #Prompt the user for the message
    message = input("Please enter the message you would like to encode: ")

    #Loop through message 
    for ch in message:

        #Print and calculate the new value of message 
        print(ord(ch) * 6 - 5, end = " ")

        



    #Open a the file “encryptedmessage.txt”
    outfile = open("encryptedmessage.txt", "w")

  

    #Write to file “encryptedmessage.txt”
    

    
    #Close the file
    outfile.close
Tourbillion
Newbie Poster
9 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Should I leave the orignal ASCII numbers intact without adding the operators into the print statement ? then somehow add the operators when I make the new variable newMessage? Even so, I would not even know how to get into that... ::FRUSTRATED!!!::

Tourbillion
Newbie Poster
9 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

What I need is a way to create another variable newMessage taking

ord(ch),end = " "

and making newMessage = ord(ch) * 6 - 5,end = " "

I only want newMessage outputted to the file. I was originally trying to just make it a one step thing. I just don't know what to do...

Tourbillion
Newbie Poster
9 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 


A hint:
(Im a newbie too:D)

message = raw_input('Enter Message:')

newMessage = ''
for char in message:
    crypted = ord(char)
    newMessage += str(crypted)

outfile = open('CryptedMessage.txt', 'w')
outfile.write(newMessage)
outfile.close()
M.S.
Junior Poster in Training
56 posts since Jul 2011
Reputation Points: 28
Solved Threads: 7
 

Maybe I'm just dense , but I tried to use this and my output into file is not coming up the same as in printing in python. The print in python is correct.

Tourbillion
Newbie Poster
9 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Here is that I have but I can't seem to get the output that i need in the file. It does work. But I need spaces inbetween each letter. So I can use a decoder later on to decode the message using the split function.

#declare and initialize variables
    #string message
    message = newMessage = encoded = " "
    #Intro
   
    print(",---.                   |              ,---.                              ")          
    print("|--- ,---.,---.,---.,---|,---.,---.    |---',---.,---.,---.,---.,---.,-.-.")
    print("|    |   ||    |   ||   ||---'|        |    |    |   ||   ||    ,---|| | |")
    print("`---'`   '`---'`---'`---'`---'`        `    `    `---'`---|`    `---^` ' '")
    print("                                                      `---'               ")

    

    #Prompt the user for the message
    message = input("Please enter the message you would like to encode: ")

    #Loop through message 
    for ch in message:
        encoded = ord(ch)* 6 - 5
        newMessage += str(encoded)

        #Print and calculate the new value of message 
        print(ord(ch) * 6 - 5, end = " ")

        

    
                


    #Open a the file “encryptedmessage.txt”
    outfile = open("encryptedmessage.txt", "w")

  

    #Write to file “encryptedmessage.txt”
    outfile.write(newMessage)
    

    
    #Close the file
    outfile.close
Tourbillion
Newbie Poster
9 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

427 601 643 643 661 <--- Output in python
427601643643661 <--- Output in .txt file

I need them to both look like the output in Python. I appreciate any input at all, I appreciate your response in helping me out. I feel like i'm going nuts, even though it is probably something tiny im overlooking. Also that "+=" what is that? We have not covered this at all. Is there another way of possibly doing it without using this. I just simply don't want to copy someones own work without knowing what it is or without the knowledge of applying something like that

Tourbillion
Newbie Poster
9 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Let's wait for masters to give you a more efficient Hint.
but for now this works for me:

#declare and initialize variables
#string message
newMessage = "" # why three variables
#Intro

print(",---.                   |              ,---.                              ")
print("|--- ,---.,---.,---.,---|,---.,---.    |---',---.,---.,---.,---.,---.,-.-.")
print("|    |   ||    |   ||   ||---'|        |    |    |   ||   ||    ,---|| | |")
print("`---'`   '`---'`---'`---'`---'`        `    `    `---'`---|`    `---^` ' '")
print("                                                      `---'               ")


#Prompt the user for the message
message = raw_input("Please enter the message you would like to encode: ")# Or just input in python 3.x

#Loop through message
for ch in message:
    encoded = ord(ch)* 6 - 5
    newMessage += str(encoded)+ " "

#Print and calculate the new value of message
print(newMessage)

#Open a the file “encryptedmessage.txt”
outfile = open("encryptedmessage.txt", "w")

#Write to file “encryptedmessage.txt”
outfile.write(newMessage)

#Close the file
outfile.close()
M.S.
Junior Poster in Training
56 posts since Jul 2011
Reputation Points: 28
Solved Threads: 7
 
Also that "+=" what is that? We have not covered this at all. Is there another way of possibly doing it without using this.

that "+=" means add something to original variable.
You can use something like this: newMessage = newMessage + (str(encoded) + " ")

a = "Hello"
a+=" World"
print a

#is the same with:

a = "Hello"
b = " World"
a = a + b

print a


But as I said Lets wait for masters' reply

M.S.
Junior Poster in Training
56 posts since Jul 2011
Reputation Points: 28
Solved Threads: 7
 

Thank you a lot! I do appreciate your help very much. It ended up working out exactly how I wanted it to.

My only other issue is that the output into the text file starts off with a space. not a huge deal. But can't seem to find why. Maybe it is normal?

Tourbillion
Newbie Poster
9 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

oh sorry I didnt noticed the space(" " instead of "") char in newMessage's default value.
I corrected it and its OK now.

M.S.
Junior Poster in Training
56 posts since Jul 2011
Reputation Points: 28
Solved Threads: 7
 

You are probably supposed to write out unicode characters not numbers in string format. See unichr function.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: