Accepting text input

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

    Accepting text input

    I'm pretty new to Python, but this has really bugged me. I can't find a
    way around it.


    The problem is that, when I use raw_input("sajf asjdf") whatever, or
    input("dsjfadsj fa"), you can only have numerical values as answers.

    Any help would be appreciated. Thanks.
  • Collin

    #2
    Re: Accepting text input

    Collin wrote:
    I'm pretty new to Python, but this has really bugged me. I can't find a
    way around it.
    >
    >
    The problem is that, when I use raw_input("sajf asjdf") whatever, or
    input("dsjfadsj fa"), you can only have numerical values as answers.
    >
    Any help would be appreciated. Thanks.

    Oh, wow. I feel so stupid. Please disregard this message. <_<

    I read the error message just now a bit more carefully, and I tried
    something. I tried defining "yes" as some random numerical value. Then
    when I did:
    (example code)

    yes = 123123983 #some number
    test = input("Test test test ")
    if test == yes:
    print "It worked."
    else:
    print "failed"

    (example code off)

    Collin

    Comment

    • Gabriel Genellina

      #3
      Re: Accepting text input

      En Mon, 12 May 2008 01:54:28 -0300, Collin <collinyeung@sh aw.caescribió:
      Collin wrote:
      >I'm pretty new to Python, but this has really bugged me. I can't find a
      >way around it.
      >>
      >>
      >The problem is that, when I use raw_input("sajf asjdf") whatever, or
      >input("dsjfads jfa"), you can only have numerical values as answers.
      >>
      >Any help would be appreciated. Thanks.
      >
      >
      Oh, wow. I feel so stupid. Please disregard this message. <_<
      No need to apologize...
      I read the error message just now a bit more carefully, and I tried
      something. I tried defining "yes" as some random numerical value. Then
      when I did:
      (example code)
      >
      yes = 123123983 #some number
      test = input("Test test test ")
      if test == yes:
      print "It worked."
      else:
      print "failed"
      >
      (example code off)
      The usual way for Python<3.0 is:

      answer = raw_input("Test test test ").lower()
      if answer == "yes":
      ...

      The input() function evaluates user input as an expression: if he types 2+5 the input() function returns the integer 7. I would never use input() in a program - it's way too unsafe; use always raw_input instead.

      --
      Gabriel Genellina

      Comment

      • Collin

        #4
        Re: Accepting text input

        Gabriel Genellina wrote:
        En Mon, 12 May 2008 01:54:28 -0300, Collin <collinyeung@sh aw.caescribió:
        >
        >Collin wrote:
        >>I'm pretty new to Python, but this has really bugged me. I can't find a
        >>way around it.
        >>>
        >>>
        >>The problem is that, when I use raw_input("sajf asjdf") whatever, or
        >>input("dsjfad sjfa"), you can only have numerical values as answers.
        >>>
        >>Any help would be appreciated. Thanks.
        >>
        >Oh, wow. I feel so stupid. Please disregard this message. <_<
        >
        No need to apologize...
        >
        >I read the error message just now a bit more carefully, and I tried
        >something. I tried defining "yes" as some random numerical value. Then
        >when I did:
        >(example code)
        >>
        >yes = 123123983 #some number
        >test = input("Test test test ")
        >if test == yes:
        > print "It worked."
        >else:
        > print "failed"
        >>
        >(example code off)
        >
        The usual way for Python<3.0 is:
        >
        answer = raw_input("Test test test ").lower()
        if answer == "yes":
        ...
        >
        The input() function evaluates user input as an expression: if he types 2+5 the input() function returns the integer 7. I would never use input() in a program - it's way too unsafe; use always raw_input instead.
        >
        If I use it like that, do I have to import anything to have the .lower()
        work? And if I do, what does the .lower() signify?

        Comment

        • Kam-Hung Soh

          #5
          Re: Accepting text input

          On Wed, 14 May 2008 11:02:36 +1000, Collin <collinyeung@sh aw.cawrote:
          Gabriel Genellina wrote:
          >En Mon, 12 May 2008 01:54:28 -0300, Collin <collinyeung@sh aw.ca
          >escribió:
          >>
          >>Collin wrote:
          >>>I'm pretty new to Python, but this has really bugged me. I can't find
          >>>a
          >>>way around it.
          >>>>
          >>>>
          >>>The problem is that, when I use raw_input("sajf asjdf") whatever, or
          >>>input("dsjfa dsjfa"), you can only have numerical values as answers.
          >>>>
          >>>Any help would be appreciated. Thanks.
          >>>
          >>Oh, wow. I feel so stupid. Please disregard this message. <_<
          > No need to apologize...
          >>
          >>I read the error message just now a bit more carefully, and I tried
          >>something. I tried defining "yes" as some random numerical value. Then
          >>when I did:
          >>(example code)
          >>>
          >>yes = 123123983 #some number
          >>test = input("Test test test ")
          >>if test == yes:
          >> print "It worked."
          >>else:
          >> print "failed"
          >>>
          >>(example code off)
          > The usual way for Python<3.0 is:
          > answer = raw_input("Test test test ").lower()
          >if answer == "yes":
          > ...
          > The input() function evaluates user input as an expression: if he
          >types 2+5 the input() function returns the integer 7. I would never use
          >input() in a program - it's way too unsafe; use always raw_input
          >instead.
          >>
          >
          If I use it like that, do I have to import anything to have the .lower()
          work? And if I do, what does the .lower() signify?
          --

          >
          You don't need to import any module to use ".lower()"; it is a method ofa
          string. raw_input() returns a string, so you can use methods of a string.

          Try the following statement to see what happens:
          "ABCDE".low er()

          --
          Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

          Comment

          • Collin

            #6
            Re: Accepting text input

            Kam-Hung Soh wrote:
            On Wed, 14 May 2008 11:02:36 +1000, Collin <collinyeung@sh aw.cawrote:
            >
            >Gabriel Genellina wrote:
            >>En Mon, 12 May 2008 01:54:28 -0300, Collin <collinyeung@sh aw.ca>
            >>escribió:
            >>>
            >>>Collin wrote:
            >>>>I'm pretty new to Python, but this has really bugged me. I can't
            >>>>find a
            >>>>way around it.
            >>>>>
            >>>>>
            >>>>The problem is that, when I use raw_input("sajf asjdf") whatever, or
            >>>>input("dsjf adsjfa"), you can only have numerical values as answers.
            >>>>>
            >>>>Any help would be appreciated. Thanks.
            >>>>
            >>>Oh, wow. I feel so stupid. Please disregard this message. <_<
            >> No need to apologize...
            >>>
            >>>I read the error message just now a bit more carefully, and I tried
            >>>something. I tried defining "yes" as some random numerical value. Then
            >>>when I did:
            >>>(example code)
            >>>>
            >>>yes = 123123983 #some number
            >>>test = input("Test test test ")
            >>>if test == yes:
            >>> print "It worked."
            >>>else:
            >>> print "failed"
            >>>>
            >>>(example code off)
            >> The usual way for Python<3.0 is:
            >> answer = raw_input("Test test test ").lower()
            >>if answer == "yes":
            >> ...
            >> The input() function evaluates user input as an expression: if he
            >>types 2+5 the input() function returns the integer 7. I would never
            >>use input() in a program - it's way too unsafe; use always raw_input
            >>instead.
            >>>
            >>
            >If I use it like that, do I have to import anything to have the
            >.lower() work? And if I do, what does the .lower() signify?
            >--
            >http://mail.python.org/mailman/listinfo/python-list
            >>
            >
            You don't need to import any module to use ".lower()"; it is a method of
            a string. raw_input() returns a string, so you can use methods of a
            string.
            >
            Try the following statement to see what happens:
            "ABCDE".low er()
            >
            So the .lower() string method is just to convert the string to lowercase
            letters so that you don't have to type a bunch of if - then statements
            in both cases, I'm assuming?

            Comment

            • John Salerno

              #7
              Re: Accepting text input

              On Thu, 15 May 2008 02:36:29 GMT
              Collin <collinyeung@sh aw.cawrote:
              So the .lower() string method is just to convert the string to lowercase
              letters so that you don't have to type a bunch of if - then statements
              in both cases, I'm assuming?
              You can also type:

              dir(str)

              to get a list of all the methods you can call on a string object. If you see anything interesting, then type:

              help(str.<metho d_name>) # e.g. help(str.split)

              to find out how it works. :)

              Comment

              • Kam-Hung Soh

                #8
                Re: Accepting text input

                On Thu, 15 May 2008 12:36:29 +1000, Collin <collinyeung@sh aw.cawrote:
                Kam-Hung Soh wrote:
                >On Wed, 14 May 2008 11:02:36 +1000, Collin <collinyeung@sh aw.cawrote:
                >>
                >>Gabriel Genellina wrote:
                >>>En Mon, 12 May 2008 01:54:28 -0300, Collin <collinyeung@sh aw.ca
                >>>escribió:
                >>>>
                >>>>Collin wrote:
                >>>>>I'm pretty new to Python, but this has really bugged me. I can't
                >>>>>find a
                >>>>>way around it.
                >>>>>>
                >>>>>>
                >>>>>The problem is that, when I use raw_input("sajf asjdf") whatever, or
                >>>>>input("dsj fadsjfa"), you can only have numerical values as answers.
                >>>>>>
                >>>>>Any help would be appreciated. Thanks.
                >>>>>
                >>>>Oh, wow. I feel so stupid. Please disregard this message. <_<
                >>> No need to apologize...
                >>>>
                >>>>I read the error message just now a bit more carefully, and I tried
                >>>>something . I tried defining "yes" as some random numerical value.
                >>>>Then
                >>>>when I did:
                >>>>(example code)
                >>>>>
                >>>>yes = 123123983 #some number
                >>>>test = input("Test test test ")
                >>>>if test == yes:
                >>>> print "It worked."
                >>>>else:
                >>>> print "failed"
                >>>>>
                >>>>(example code off)
                >>> The usual way for Python<3.0 is:
                >>> answer = raw_input("Test test test ").lower()
                >>>if answer == "yes":
                >>> ...
                >>> The input() function evaluates user input as an expression: if he
                >>>types 2+5 the input() function returns the integer 7. I would never
                >>>use input() in a program - it's way too unsafe; use always raw_input
                >>>instead.
                >>>>
                >>>
                >>If I use it like that, do I have to import anything to have the
                >>.lower() work? And if I do, what does the .lower() signify?
                >>-- http://mail.python.org/mailman/listinfo/python-list
                >>>
                > You don't need to import any module to use ".lower()"; it is a method
                >of a string. raw_input() returns a string, so you can use methods ofa
                >string.
                > Try the following statement to see what happens:
                >"ABCDE".lower( )
                >>
                >
                So the .lower() string method is just to convert the string to lowercase
                letters so that you don't have to type a bunch of if - then statements
                in both cases, I'm assuming?
                --

                >
                That's right. If you normalize your input to all lower case or upper
                case, you make it easier to process user input.

                Regards,

                --
                Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

                Comment

                Working...