#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    Help with process, involving lists, repetition


    I need to input a string of text and have each letter in that string shift to the right one place in the alphabet.

    input: abc def
    output: bcd efg

    Any ideas on how to do this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Here's what you need to help you start with. Let us know what you could come up with:

    Code:
    >>> ord('a')
    97
    >>> chr(ord('a') + 1)
    'b'
    >>>
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    In j ( www.jsoftware.com ) :
    Code:
       >:&.(a.&i.)'abcdef'  NB. interactive j prompt is 3 spaces
    bcdefg
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by noobie1000
    Here's what you need to help you start with. Let us know what you could come up with:

    Code:
    >>> ord('a')
    97
    >>> chr(ord('a') + 1)
    'b'
    >>>

    It looks like it only accepts one character. It works if only one is input but if I input more than that I get an error saying it was expecting a character but instead got a string of 13...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Originally Posted by xrlrider08
    It looks like it only accepts one character. It works if only one is input but if I input more than that I get an error saying it was expecting a character but instead got a string of 13...
    Well, that was just a hint to help you start with. Don't expect spoon-fed solutions on-demand. Please give it a try and let us know what you could come up with. If you're not able to proceed, let us know where you're stuck and we could help you. (Another hint: use loops)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    def main():
    from string import ascii_lowercase
    L = list(ascii_lowercase)

    text = input("What is your message mofo?")
    wordlist = list(text)

    for wordlist in L:
    char_position()




    print (wordlist)
    #print (char_position(text))
    print (text)


    def char_position(letter):
    return ord(letter) - 96
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    off the mark


    Here's an algorithm. At the end I made a few comments about your effort. Only a few because your interest level seems low. I'm pretty sure I'd write the program as just a few statements.
    Code:
    prepare some array
    
    for letter in text:
        if the letter is lower case
            if the letter is 'z'
                encrypted_letter = 'a'
            else
                get the ordinal value
                add 1
                encrypted_letter = convert back to a character
        store the encrypted_letter into the right position in some array
    
    return ''.join(some array)
    
    
    
    
    #def main():
    #    from string import ascii_lowercase
    #    L = list(ascii_lowercase)             # converting strings to lists for the purpose of indexing is not useful.
    #
    #    text = input("What is your message mofo?")
    #    wordlist = list(text)
    #
    #    for wordlist in L:  # wordlist takes, in turn, each object in list L, (1 lower case letter at a time)
    #        char_position()  # function call has no useful external effect.
    #        
    #
    #    
    #    
    #    print (wordlist)
    #    #print (char_position(text))
    #    print (text)
    #    
    #
    #def char_position(letter):
    #    return ord(letter) - 96
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    i have and understand the code to return the char position. I understand the boolean to handle "z". I know how to break up the words in string into characters and put them in a list. What is killing me is that my char_position function, will only accept one character at a time. I don't know how to send it one letter at a time.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Originally Posted by xrlrider08
    What is killing me is that my char_position function, will only accept one character at a time. I don't know how to send it one letter at a time.
    I didn't understand. Could you please elaborate?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by noobie1000
    I didn't understand. Could you please elaborate?
    when run, the following code ouputs the positional value of the character input + 1. This is what I want but if I input more than one character the program will only output the ordinal value of the last character in the string. I thought I was getting close but this bug is eluding me. Thank you both for the help. I have learned a something already. Any idea how to get it do output the ordinal value for each character in the input?


    def main():

    text = input("What is your message mofo?")
    wordlist = list(text)

    for letter in text:
    if letter == "z":
    encrypt_letter = "a"
    else:
    for letter in text:
    encrypt_letter = (char_to_position(letter))







    print (wordlist)
    print (encrypt_letter)





    def char_to_position(letter):
    return ord(letter) - 96

    def pos_to_char(pos):
    return chr(pos + 96)
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    This runs but has bugs


    Originally Posted by xrlrider08
    when run, the following code ouputs the positional value of the character input + 1. This is what I want but if I input more than one character the program will only output the ordinal value of the last character in the string. I thought I was getting close but this bug is eluding me. Thank you both for the help. I have learned a something already. Any idea how to get it do output the ordinal value for each character in the input?


    def main():

    text = input("What is your message mofo?")
    wordlist = list(text)

    for letter in text:
    if letter == "z":
    encrypt_letter = "a"
    else:
    for letter in text:
    encrypt_letter = (char_to_position(letter))







    print (wordlist)
    print (encrypt_letter)





    def char_to_position(letter):
    return ord(letter) - 96

    def pos_to_char(pos):
    return chr(pos + 96)

    a space returns "!"
    input two characters makes the loop run twice...
    in: gh
    out: hihi

    in: gh gh
    out: hi!hihi!hihi!hihi!hi

    I am beginning to wonder if there is a completely different process that would be much better to handle this problem



    #str.translate(table[, ])????????
    #list characters a-z? 26 - 25 for index?

    #close, but buggy

    def main():

    text = input("What is your message mofo?")
    wordlist = list(text)

    output = []

    for letter in text:
    if letter == "z":
    encrypt_letter = "a"
    elif letter == " ":
    encrypt_letter = ""

    else:
    for letter in text:
    ordvalue = (char_to_position(letter))
    encrypt_letter = pos_to_char(ordvalue)
    output.append(encrypt_letter)






    print (ordvalue)
    #print (wordlist)
    #print (encrypt_letter)
    print (''.join(output))





    def char_to_position(letter):
    return ord(letter) - 96

    def pos_to_char(pos):
    return chr(pos + 97)

    '''
    def final(letter):
    from string import ascii_lowercase
    L = list(ascii_lowercase)
    '''
    main()
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    final answer, for your entertainment


    def main():

    text = input("What is your message mofo?")
    wordlist = list(text)

    output = []

    '''
    for letter in text:
    if letter == "z":
    encrypt_letter = "a"
    elif letter == " ":
    encrypt_letter = ""


    else:
    x = 0
    while x < len(text):
    ordvalue = (char_to_position(letter))
    encrypt_letter = pos_to_char(ordvalue)
    output.append(encrypt_letter)
    x += 1

    '''

    x = 0
    while x < len(text):
    if text[x] == "z":
    encrypt_letter = "a"
    x += 1
    output.append(encrypt_letter)


    elif text[x] == " ":
    encrypt_letter = chr(32)
    output.append(encrypt_letter)
    x += 1

    else:
    ordvalue = (char_to_position(text[x]))
    encrypt_letter = pos_to_char(ordvalue)
    output.append(encrypt_letter)
    x += 1






    #print (ordvalue)
    #print (wordlist)
    #print (encrypt_letter)
    print (''.join(output))





    def char_to_position(letter):
    return ord(letter) - 96

    def pos_to_char(pos):
    return chr(pos + 97)

    '''
    def final(letter):
    from string import ascii_lowercase
    L = list(ascii_lowercase)
    '''
    main()
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Originally Posted by xrlrider08
    def main():
    text = input("What is your message mofo?")
    wordlist = list(text)
    This is not required, since a string is already treated as a sequence.

    See if this helps:

    Code:
    text = input("What is your message mofo?\n")
    for c in text:
        if c.isalpha() and c != 'z':
            print(chr(ord(c) + 1), end='')
        elif c == 'z':
            print('a', end='')
        else:
            print(c, end='')
    Code:
    >>> ================================ RESTART ================================
    >>> 
    What is your message mofo?
    this is my message
    uijt jt nz nfttbhf
    >>> 
    >>> ================================ RESTART ================================
    >>> 
    What is your message mofo?
    this is my message 1234 !@#$
    uijt jt nz nfttbhf 1234 !@#$
    >>>
    Last edited by noobie1000; November 18th, 2013 at 09:27 PM.
  26. #14
  27. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Oh, I didn't know of the chr function. I've always used

    '%c'%asciiValue

    >>> '{:c}'.format(33)
    '!'
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo