2

i have a simple program where you put in the ascii code to get a message, bit i keep getting a error

    asciiNum = eval(numStr)
   File "<string>", line 0

    ^
  SyntaxError: unexpected EOF while parsing


#    numbers2text.py
#     A program to convert a sequence of ASCII numbers into
#         a string of text.

import string  # include string library for the split function.


def main():
    print ("This program converts a sequence of ASCII numbers into")
    print  ("the string of text that it represents.")
    print   ()

    # Get the message to encode
    inString = input("Please enter the ASCII-encoded message: ")

    # Loop through each substring and build ASCII message
    message = ""
    for numStr in inString.split(inString):
        # convert the (sub)string to a number
        asciiNum = eval(numStr)
        # append character to message
        message = message + chr(asciiNum)

    print ("The decoded message is:", message)


main()
Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
user6537433
  • 23
  • 11
  • 4
    `inString.split(inString)` You're splitting the string based on itself. That will always return`['', '']`. Also, you REALLY shouldn't be using eval. You can just use `int` here. – Morgan Thrapp Jul 12 '16 at 19:52
  • 1
    even if i do use int i still get a error – user6537433 Jul 12 '16 at 19:57
  • Yes, because that's not the main issue. The issue is that you're doing `'abc'.split('abc')`, which tries split `abc` based on the delimiter `abc`. What are you trying to do with that call to `split`? – Morgan Thrapp Jul 12 '16 at 19:57
  • Please give an example of what the user is supposed to type when the "Please enter the ASCII-encoded message: " prompt appears. – Kevin Jul 12 '16 at 20:01
  • the ascii code that would be entered: example:67 83 49 50 48 32 105 115 32 102 117 110 33 ill get a return of : CS120 is fun! – user6537433 Jul 12 '16 at 20:03
  • 4
    Are you using [this](https://books.google.com/books?id=aJQILlLxRmAC&lpg=PA93&ots=CA9NxGSdXD&dq=%22This%20program%20converts%20a%20sequence%20of%20ASCII%20numbers%20into%22&pg=PA93#v=onepage&q=%22This%20program%20converts%20a%20sequence%20of%20ASCII%20numbers%20into%22&f=false) text book to learn Python? If so, please use a different one, it's really bad. – Morgan Thrapp Jul 12 '16 at 20:03
  • 2
    Take a look at [this collection of tutorials](http://sopython.com/wiki/What_tutorial_should_I_read%3F). That textbook is teaching exceptionally bad practices. – Morgan Thrapp Jul 12 '16 at 20:04
  • 3
    @MorganThrapp: "really bad" is a very benevolent description for that book. – Matthias Jul 12 '16 at 20:13
  • 1
    @Matthias I'm trying not to get my comment flagged, or I'd have a few choice words for anyone who advocates camelCase and liberal usage of ~shudder~ eval. ;) – Morgan Thrapp Jul 12 '16 at 20:16
  • 3
    Also to add directly to the case against that ***horrible*** book which uses eval all over the place (so you really know we mean it when we say don't do it), see: [Is Using Eval in Python Bad](http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice) and Ned Batchelder's [Eval Really is Dangerous](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) – LinkBerest Jul 12 '16 at 20:19

0 Answers0