SyntaxError:分析时出现意外的EOF,eval和string出错

2024-09-22 16:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的程序,你把ascii码放进去,得到一条信息,而我总是得到一个错误

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

Tags: ofthetomessageconvertstringevalascii