error with string (beginner)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Pavluck

    #1

    error with string (beginner)

    Hello. I get the following error with the following code. Is there
    something wrong with my Python installation?

    code:
    import types
    something = input("Enter something and I will tell you the type: ")

    if type(something) is types.IntType:
    print "you entered an integer"
    elif type(something) is types.StringTyp e:
    print "you entered a string"

    error:
    String: Source for exec/eval is unavailable

  • K.S.Sreeram

    #2
    Re: error with string (beginner)

    Alex Pavluck wrote:[color=blue]
    > String: Source for exec/eval is unavailable[/color]

    I'm not able to find this message anywhere in the Python-2.4.3 sources.
    What python version are you using? What input did you enter when the
    prompt appeared? and copy&paste the *exact* exception you got including
    the full traceback.

    Regards
    Sreeram


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2.2 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFEnpwXrgn 0plK5qqURAk7fAJ 9I449qtRT8HbpHF ScM6RcbTGL8HwCe P9+G
    YM8xbP7i0cyS1Dy a56nAv4c=
    =X7Ek
    -----END PGP SIGNATURE-----

    Comment

    • Jon Clements

      #3
      Re: error with string (beginner)


      Alex Pavluck wrote:[color=blue]
      > Hello. I get the following error with the following code. Is there
      > something wrong with my Python installation?
      >
      > code:
      > import types
      > something = input("Enter something and I will tell you the type: ")
      >
      > if type(something) is types.IntType:
      > print "you entered an integer"
      > elif type(something) is types.StringTyp e:
      > print "you entered a string"
      >
      > error:
      > String: Source for exec/eval is unavailable[/color]
      [color=blue]
      >From the docs:[/color]
      """
      input( [prompt])

      Equivalent to eval(raw_input( prompt)). Warning: This function is not
      safe from user errors! It expects a valid Python expression as input;
      if the input is not syntactically valid, a SyntaxError will be raised.
      Other exceptions may be raised if there is an error during evaluation.
      (On the other hand, sometimes this is exactly what you need when
      writing a quick script for expert use.)
      If the readline module was loaded, then input() will use it to provide
      elaborate line editing and history features.

      Consider using the raw_input() function for general input from users.
      """"

      So you want be using raw_input() for starters...

      Cheers,

      Jon.

      Comment

      • Jason

        #4
        Re: error with string (beginner)

        I believe what you are trying to do is something like the following.

        Code:
        def isIntLike(x):
        try:    int(x)
        except: return False
        else:   return True
        
        something = raw_input("Enter something and I will tell you the type: ")
        
        if isIntLike(something):	print "I am an int"
        elif isinstance(something, type('')):	print "I am a string"
        else:	print "I am an impostor!"
        Note that you should be using raw_input() rather than input(). The
        former always returns a string. The latter tries to execute the input,
        hence your error message.

        Also note that everything input on the command line is a string,
        regardless of what you type. If you want type checking, use C++ or
        java. If you want to check for anything more than IntLike strings, you
        should consider using regular expressions through the 're' package.
        For user-defined types, use 'isinstance(som ething, class object)' to
        check for a type.

        Lastly, you may be interested in the 'getopt' module, which handles
        command line arguments (as opposed to prompting user for input)
        automatically. See here:


        Check out the Python Cookbook for lots of good examples:


        HTH,

        Jay



        Alex Pavluck wrote:[color=blue]
        > Hello. I get the following error with the following code. Is there
        > something wrong with my Python installation?
        >
        > code:
        > import types
        > something = input("Enter something and I will tell you the type: ")
        >
        > if type(something) is types.IntType:
        > print "you entered an integer"
        > elif type(something) is types.StringTyp e:
        > print "you entered a string"
        >
        > error:
        > String: Source for exec/eval is unavailable[/color]

        Comment

        • Cameron Laird

          #5
          Re: error with string (beginner)

          In article <1151240297.725 336.305960@i40g 2000cwc.googleg roups.com>,
          Alex Pavluck <apavluck@gmail .com> wrote:[color=blue]
          >Hello. I get the following error with the following code. Is there
          >something wrong with my Python installation?
          >
          >code:
          >import types
          >something = input("Enter something and I will tell you the type: ")
          >
          >if type(something) is types.IntType:
          > print "you entered an integer"
          >elif type(something) is types.StringTyp e:
          > print "you entered a string"
          >
          >error:
          >String: Source for exec/eval is unavailable
          >[/color]

          There are several things wrong--'least three distinct ones, by my count.

          What version of Python do you believe you're using? What input do you
          enter at the "Enter something ..." prompt?

          I suspect you're going to have happier results all around when you
          replace "input" with "raw_input" . I encourage you to read the documenta-
          tion for each.

          Comment

          • Bruno Desthuilliers

            #6
            Re: error with string (beginner)

            Jason wrote:[color=blue]
            > I believe what you are trying to do is something like the following.
            >
            >
            Code:
            > def isIntLike(x):
            > 	try:    int(x)
            > 	except: return False[/color]
            
            *Never* ever use a bare except clause. *Always* specify wich exceptions
            you are expecting. (NB : here, TypeError and ValueError).
            
            (NB :  of course, like for all and any do's-and-don't rules, there are
            actually a very few cases where using a bare except may be ok.)
            [color=blue]
            > 	else:   return True
            >
            > something = raw_input("Enter something and I will tell you the type: ")
            >
            > if isIntLike(something):	print "I am an int"[/color]
            
            Nope. At this stage, 'something' is *not* an int - it's a string that
            can be turned into an int.
            [color=blue]
            > elif isinstance(something, type('')):	print "I am a string"[/color]
            
            You don't need the "type('')" stuff - use isinstance(something,
            basestring) instead.
            [color=blue]
            > else:	print "I am an impostor!"
            >
            [/color]

            (snip)

            --
            bruno desthuilliers
            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
            p in 'onurb@xiludom. gro'.split('@')])"

            Comment

            Working...