python challenge

Started by
3 comments, last by Hollower 16 years, 9 months ago
okay well i started python challenge and i did the warmup etc. i got to the first one, from looking at the hint thing the letters are just + 2 and find the new letter. so i want to make a program where i can just type in the whole message and have it decoded from that. i made it or at least i thought i did but when i try to do it im getting errors. what did i do wrong? heres my code

#decodes pygame challenge message
# enter letter change to number add 2 then convert back to letter
# display message
import string
def main():
    message = raw_input("Enter your message.\n")

    for ch in message.split():
        value = ord(ch) + 2
        newLetter = chr(value)
        Nmessage += newLetter
    print Nmessage,

heres the error message i get

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    main()
  File "C:/Python25/pygameDecoder.py", line 9, in main
    value = ord(ch) + 2
TypeError: ord() expected a character, but string of length 5 found

obviously its trying to ge the numeric ordinate of the whole word but shouldnt ch mean just the charcter? isnt that looping it through each character of the split words? any help is appreciated thanks heyhohey
Advertisement
By default split splits on whitespace - so ch iterates over each word in the input. You'll need to iterate over the individual characters instead
Change

for ch in message.split():

to

for ch in message:

string.split() returns a list of strings, not a list of characters. So what the first does is iterate over strings. Instead, you can treat the message as an iterable object and just iterate over that, which should have the behavior you want.
i still dont understand after reading both of your posts. if i only go through chs and dont split it then it will take the value of spaces in ascii and add 2 and find the character for that and i dont want that. so how do i split the words then for each word loop through the characters? do i need to split it before the loop? but i dont know the number of words the user will enter(even though i know how many words i will enter for the message in the challenge id like it to be functional for anything).

thanks
heyhohey

oh and btw i thought if i did
for ch in message.split():
it would split the words on white space then go through each ch in the string.
but cshowe said it does the strings. how do i make it just the chs? i thought ch in a loop just by default automatically means characters... am i mistaken?

Quote:Original post by HeyHoHey
i thought ch in a loop just by default automatically means characters... am i mistaken?


Yes. ch doesn't mean anything special. It's your variable name. Any name you put after the for is the name you want to bind to each element of the iterable. If the iterable is a string, each element will be a character. If the iterable is a list, each element will be whatever the list element is. If it's a list of words, each element will be a word. You could simply iterate over the string and put in
if ch.isspace():    continue
As an aside: Python doesn't have a character type. A character is a string of length 1.

This topic is closed to new replies.

Advertisement