Using len()

C

ctilly

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
 
B

Bryan Olson

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 [...]
But this throws the following error and I have no idea why. Please
enlighten.

My error ==> len() of unsized object

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

http://docs.python.org/lib/built-in-funcs.html

Try "rawinput()" instead.
 
J

Jonathan Gardner

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.
 
S

Steven D'Aprano

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top