0

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.

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by 4pennies
0

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

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.