Hello, a bit new to python here and would appreciate some help.

I am writing a simple encoder to a raw_input(turn to numerical ascii and then applying some numbers to it). I was able to get the encoding right but when i try to do a outfile only the first number is outputted.

What i have is this:

def encode(): 
    mess = raw_input ("Enter message:")
    for i in mess:
        nummess = ord (i) * 2 * 7 + 3
        stringnum = str(nummess)
        joinnum = stringnum + ","
        outfile = open ("encodemess.txt", "w")
        outfile.write (joinnum)
        outfile.close()

My goal is to have somthing like this show up in the .txt;
"hello" would be:
1459, 1417, 1515, 1515, 1557,
but atm, only "1459," its outputted to the .txt.
I am quite sure it is something simple, just cant put my finger on.

Starting with and empty string, you have to build up your code string in the loop, and then write it to the file outside the loop:

def encode(): 
    mess = raw_input ("Enter message:")
    joinnum = ""
    for i in mess:
        nummess = ord (i) * 2 * 7 + 3
        stringnum = str(nummess)
        joinnum += stringnum + ","
    
    print(joinnum)  # for testing only
    outfile = open ("encodemess.txt", "w")
    outfile.write (joinnum)
    outfile.close()


encode()

Starting with and empty string, you have to build up your code string in the loop, and then write it to the file outside the loop:

def encode(): 
    mess = raw_input ("Enter message:")
    joinnum = ""
    for i in mess:
        nummess = ord (i) * 2 * 7 + 3
        stringnum = str(nummess)
        joinnum += stringnum + ","
    
    print(joinnum)  # for testing only
    outfile = open ("encodemess.txt", "w")
    outfile.write (joinnum)
    outfile.close()


encode()

ahh, thanks that did it, i thought it was something simple.

This article has been dead for over six months. Start a new discussion instead.