Solved

# Loop repitition

Posted on 2005-05-06
1,216 Views
I need to ask again for a INPUT from the user if the user typed wrongly

for example

I ask the user for Input by

length = getInput("Enter the Length","100",)

here getInput is a ABAQUS Function it returns the value if the user pressed OK Button and 'None' if the user pressed CANCEL button.

now the problem is if the user enters some absurd values say 1000000 then i need to ask again the user for a correct value

the correct value is (100 - 1000).

I had written a small program but it is not working.

can anybody help me out for a solution.
###########################
#Program
###########################
length = getInput('Enter the Length for the Rectangle','150')
for i in range(0,1):
...if length != None and length !=" " and length>=100 and length<=1000 :
......break
...else:
......i=0
......length= getInput('Enter the Length','150')
......continue

print "Length=" ,length
########################################
#End
########################################
0
Question by:ashok3sep

LVL 15

Expert Comment

length=0
while length is not None and (length<100 or length>1000):
length=getInput('Enter the Length','150')
0

LVL 17

Expert Comment

Which can be simplified to:
length=0
while length and not 100 < length < 1000:
length=getInput('Enter the Length','150')
0

LVL 14

Expert Comment

ramrom: No it can't.  You didn't run that.
0

LVL 17

Expert Comment

OOPS - well here's a new release candidate:

length=10 # or any value outside desired range
while length and not 100 <= length <= 1000:
length=getInput('Enter the Length','150')
0

LVL 5

Author Comment

Hi Experts,

SiliconLength,SiliconHeight,klebstoffHeight,length,height,angle1,angle2,angle3,angle4 = getInputs(areas,label = 'Specify Block Dimensions',dialogTitle ='Testing')
inputTuple = [SiliconLength,SiliconHeight,klebstoffHeight,length,height,angle1,angle2,angle3,angle4]

for i in range(0,len(inputTuple)):
...if inputTuple[i] == None and inputTuple[i] == ' ' :
...... SiliconLength,SiliconHeight,klebstoffHeight,length,height,angle1,angle2,angle3,angle4 = getInputs(areas,label = 'Specify Block Dimensions',dialogTitle ='Testing')
...else:
...... break
0

LVL 5

Author Comment

How can i test all the varaibles in a tuple for None or empty string
0

LVL 5

Author Comment

after that how can i test the variables Induvidually
0

LVL 17

Expert Comment

Hi ashok3sep. This sounds like a new question. For several reasons we ask that you start a new Question rather than extending the current Question.

If we have given you the answer you wanted to "Loop repitition" please accept the answer. If not, please let us know what is needed or missing.

And please open a new question for "test all the varaibles in a tuple...".

Bob Gailer
Page Editor
0

LVL 5

Author Comment

I have increased the points from 100 to 250 for the question and that is the reason i had asked for this question

if you wish to have in another thread then i can also have it in anothere thread.

With regards,

Freedom
0

LVL 17

Assisted Solution

OK I now see how your "new" question is a variation of what you started with. I missed that. There is a fine line around how much "work" to get done within the scope of one Question. Let's continue with this and see when we agree to start a new Question.

The for loop as written will always break at the first element of inputTuple since inputTuple[i] == None and inputTuple[i] == ' '  is always False.

You really need 2 loops here, an outer loop to keep asking for input until the user gets it right, and an inner looop to test each item of the tuple. Example:

good = False # inner loop changes this to indicate success
for tries in range(10): # limit number of user attempts to avoid endless loop
..inputTuple = getInputs(areas,label = 'Specify Block Dimensions',dialogTitle ='Testing')
..for item in inputTuple: # inner loop examines each item till one fails the test
....if item == None or item == '':
......break
....else: # all items passed
......good = True
..if good:
break

Now unpack the tuple:
SiliconLength,SiliconHeight,klebstoffHeight,length,height,angle1,angle2,angle3,angle4 = inputTuple

"how can i test the variables Individually?" I take this to mean "how can I give the user the name of the variable that he entered wrong?" True?

List comprehension can often replace a for loop resulting in simpler code:
for tries in range(10):
..inputTuple = getInputs(areas,label = 'Specify Block Dimensions',dialogTitle ='Testing')
..if [1 for item in inputTuple if item != None and item != '']:
....break
..else:
....print "10 strikes - you're out"
0

LVL 15

Expert Comment

That will not fly. You test that one input is OK, not all of them.

Which can be simplified to:
for tries in range(10):
inputTuple = getInputs(areas,label = 'Specify Block Dimensions',dialogTitle ='Testing')
if sum(1 for item in inputTuple if item)==len(inputTuple):
SiliconLength,SiliconHeight,klebstoffHeight,length,height,angle1,angle2,angle3,angle4 = inputTuple
# test individual values
if test_ok:
break
print "Not all vars entered correctly, try again."
else:
print "10 strikes - you're out"

Note: line with sum() is for Python 2.4, for previous version use sum([1 for item in inputTuple if item]).
0

LVL 17

Expert Comment

Oh yeah. My head is not working this AM like I'd like it to. Sigh.

But let's keep the [] for the comprehension: sum([1 for item in inputTuple if item]).
0

LVL 5

Author Comment

Thanks ramrom and mish33 for your fast responses.

let me say the pseudo code of what i need in the loop

1.) first is test in the GUI that all the fields are not EMPTY and not NONE

2.)then i test all the values entered by the user are within the limits .

for example
lets say the user entered

SiliconLength  = 100 ;
SiliconHeight  = 1000; and so on.......

first i test whether the values in the fields are filled and not EMPTY or NONE

then i take the value of SiliconLength and check whether 100 is allowed or not
if 100 is not allowed and i need a value between 50 - 75 then i need again to say the user that the value entered for silicon is wrong and ask the user again for hte input.........

Hope this is clear and understandable..
i am working araound to find a logic behind the problem but it is little difficult for me at the moment.

With regards,
Freedom.
0

LVL 15

Accepted Solution

INPUT_LIMITS = (
(10,100, 'SiliconLength'),
(10,20, SiliconHeight'),
(1,5, 'klebstoffHeight'),
(100,200, 'length'),
(50,150, 'height'),
(0,180, 'angle1'),
(0,90, 'angle2'),
(0,180, 'angle3'),
(0,90, 'angle4')
)
for tries in range(10):
inputTuple = getInputs(areas,label = 'Specify Block Dimensions',dialogTitle ='Testing')
if sum(1 for item in inputTuple if item)==len(inputTuple):
# test individual values
all_fine = True
for item, (low,high,name) in zip(inputTuple, INPUT_LIMITS):
try: x = int(item)
except:
print "Expecting int, got '%s' for %s" % (item, name)
n = 0
if not (low<=x<=high):
all_fine = False
print "%s shall be in [%d..%d] range" % (name, low, high)
if all_fine:
break
print "Not all vars entered correctly, try again."
else:
print "10 strikes - you're out"
0

## Featured Post

Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…