1

Below is a piece of code from Python which has been bothering me for a while.

var=0
while (var <1 or var>100):
 var=raw_input('Enter the block number ')
 if (var >=1 and var<=100):
 print '\nBlock Number : ',var 
 else:
 print 'ERROR!!! Enter again.'

The problem is that the while loop iterates continuously without breaking. Can anyone help me how to break the loop.

Is there any way to implement a do..while in Python?

asked Jul 18, 2012 at 14:16

5 Answers 5

12

The problem is that raw_input returns a string. You're comparing a string with an integer which you can do in python 2.x (In python 3, this sort of comparison raises a TypeError), but the result is apparently always False. To make this work you probably want something like var=int(raw_input('Enter the block number'))

From the documentation:

objects of different types always compare unequal, and are ordered consistently but arbitrarily.

answered Jul 18, 2012 at 14:17
Sign up to request clarification or add additional context in comments.

4 Comments

ints are always less than strs
@ColinDunklau -- Is that actually true? Or is that a CPython implementation detail? The only thing that actually matters is that the objects are ordered consistently.
@mgilson good point, I believe it depends on the implementation. IIRC, it's only like that in python 2.x, as well.
@ColinDunklau -- Good point about only in python2.x. I've updated my answer saying it's illegal in python 3 (and raises a TypeError)
4

You're needlessly checking var twice, and you're trying to compare int and str (because raw_input returns a string), which doesn't work right. Try this:

var=0
while True:
 var=int(raw_input('Enter the block number '))
 if (var >=1 and var<=100):
 print '\nBlock Number : ',var 
 break
 else:
 print 'ERROR!!! Enter again.'
answered Jul 18, 2012 at 14:18

Comments

1

You should convert your string to int.

var=0
while (var <1 or var>100):
 # I changed here
 var=int(raw_input('Enter the block number '))
 if (var >=1 and var<=100):
 print '\nBlock Number : ',var 
 else:
 print 'ERROR!!! Enter again.'
answered Jul 18, 2012 at 14:18

Comments

0

You're running into an issue that strings (as returned by raw_input) are always greater than integers:

>>> "25" > 100
True

You need to convert your input to an integer first:

var = int(raw_input("Enter the block number "))

Of course, you'll want to be resilient in the face of bad input, so you'll probably want to wrap the whole thing in a try block.

answered Jul 18, 2012 at 14:20

Comments

0

hello you need to enter "break" and also var should be an integer, see below

while True:
var=int(raw_input('Enter the block number '))
if (var >=1 and var<=100):
 print '\nBlock Number : ',var
 break
else:
 print 'ERROR!!! Enter again.'
 continue

hope this helps

answered Jul 18, 2012 at 14:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.