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

Recommended Answers

All 11 Replies

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!!!::

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...

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()

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.

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

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

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()

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

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?

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

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

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.