473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

Mar 11 '06 #1
6 1789
ct****@gmail.com wrote:
[...]
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.
--
--Bryan
Mar 11 '06 #2
That works, thanks. Can you tell me why the differences exist?

Mar 11 '06 #3
Bryan Olson wrote:
Try "rawinput()" instead.


or, less likely to give a NameError,

raw_input()

</F>

Mar 11 '06 #4
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.

Mar 11 '06 #5
On Sat, 11 Mar 2006 01:05:48 -0800, ctilly wrote:
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.
--
Steven.

Mar 11 '06 #6
Steven D'Aprano wrote:
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.


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

http://homepages.cwi.nl/~steven/abc/qr.html

</F>

Mar 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
7
by: Rich Milburn [MVP] | last post by:
Ok I am not a programmer, I copied some code and very painfully got it working in VB6. I can adjust the volume with waveOutSetVolume on winmm.dll. But I could not seem to be able to figure out how...
6
by: Julien | last post by:
Hello, I have some files located in a file server and managed by a SQL database from a web based interface using ASP + VBSCRIPT technology. I need to automatically copy those files to a web...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
5
by: Ole | last post by:
Hi, I have a native dll that has a byte array to which I want a pointer to in my C# application but I will need some help to do that. the dll exports this function: void DllFunction (PBYTE...
9
by: chikkubhai | last post by:
Why is the result different for the following set of two code snippets Code without using this pointer #include <string> #include <iostream> using namespace std; struct X { private:
2
by: cj | last post by:
We have a legacy accounting system (not developed in house) here that happens to be written in Visual FoxPro. One of the tables has an index that is actually a coded function COMPANY1 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.