Using len()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ctilly@gmail.com

    #1

    Using len()

    I have what I think is a very simple question. I have a Python script
    that I found that I want to tweek a little bit. All I want to do is
    add in a validator to make sure a value has been keyed into the imput
    box.

    The code currently is...
    while yourguess != mynum:

    tries = tries + 1
    yourguess = input("Your guess? ")

    if (yourguess != mynum):

    #blah blah blah

    # end of the if statement
    # repeat until user gets it right

    But I would like to change it to be something like....

    while yourguess != mynum:

    tries = tries + 1
    yourguess = input("Your guess? ")

    if len(yourguess)= =0:
    continue

    elif (yourguess != mynum):

    #blah blah blah

    # end of the if statement
    # repeat until user gets it right

    But this throws the following error and I have no idea why. Please
    enlighten.

    My error ==> len() of unsized object

  • Bryan Olson

    #2
    Re: Using len()

    ctilly@gmail.co m wrote:
    [...][color=blue]
    > But I would like to change it to be something like....
    >
    > while yourguess != mynum:
    >
    > tries = tries + 1
    > yourguess = input("Your guess? ")
    >
    > if len(yourguess)= =0:
    > continue[/color]
    [...][color=blue]
    > But this throws the following error and I have no idea why. Please
    > enlighten.
    >
    > My error ==> len() of unsized object[/color]

    The innocent sounding function "input()" actually invokes the perilous
    "eval()". Read about it at:



    Try "rawinput() " instead.


    --
    --Bryan

    Comment

    • ctilly@gmail.com

      #3
      Re: Using len()

      That works, thanks. Can you tell me why the differences exist?

      Comment

      • Fredrik Lundh

        #4
        Re: Using len()

        Bryan Olson wrote:
        [color=blue]
        > Try "rawinput() " instead.[/color]

        or, less likely to give a NameError,

        raw_input()

        </F>



        Comment

        • Jonathan Gardner

          #5
          Re: Using len()

          Methinks you are confused about the structure of your program.

          If we write the program out in plain English in the form of a recipe,
          it should look something like this:

          1. Get input.
          2. Check to see if they guessed right.
          3. If not, go back to 1.

          This structure hints at an infinite loop. The breakout condition is a
          correct guess. You always need a breakout condition, or the loop goes
          on forever. (Sometimes you do want an infinite loop. Think about how
          you would write a web server.)

          (Note on variable names: "your" and "my" are not clear. Usually, first
          person ("I", "me", "my") refers to the programmer. The second person
          ("you", "your") is rarely used. "You" could be the user, the computer,
          or another programmer. Just use third person ("it", "he", "they").

          So, we write an infinite loop in Python:

          while True:

          # Get input
          guess = raw_input()

          # Is the guess right?
          if guess == num:
          # Yep. Break out of the loop.
          break

          # Otherwise, loop again.

          You could put the breaking condition in the "while" line. But since it
          appears in the middle of the loop, this is difficult to do. So just put
          it in explicitly in the middle and call it a day.

          Comment

          • Steven D'Aprano

            #6
            Re: Using len()

            On Sat, 11 Mar 2006 01:05:48 -0800, ctilly wrote:
            [color=blue]
            > That works, thanks. Can you tell me why the differences exist?[/color]

            For those who have just joined us, "that" is using raw_input() instead of
            input().

            I don't know why input() is the equivalent of eval(raw_input( prompt)), but
            it is. Presumably Guido thought it was a good idea at the time. I recall
            hearing somewhere that Guido is planning to rename raw_input() to input()
            some time in Python 3.


            --
            Steven.

            Comment

            • Fredrik Lundh

              #7
              Re: Using len()

              Steven D'Aprano wrote:
              [color=blue]
              > I don't know why input() is the equivalent of eval(raw_input( prompt)), but
              > it is. Presumably Guido thought it was a good idea at the time.[/color]

              possibly inspired by ABC's "READ" and "READ RAW" commands:



              </F>



              Comment

              Working...