Jump to content
  • Advertisement
Sign in to follow this  
HeyHoHey

python challenge

This topic is 4133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!