[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1222
  • Last Modified:

Loop repitition

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
ashok3sep
Asked:
ashok3sep
  • 5
  • 5
  • 3
  • +1
2 Solutions
 
mish33Commented:
length=0
while length is not None and (length<100 or length>1000):
   length=getInput('Enter the Length','150')
0
 
ramromconsultant Commented:
Which can be simplified to:
length=0
while length and not 100 < length < 1000:
   length=getInput('Enter the Length','150')
0
 
RichieHindleCommented:
ramrom: No it can't.  You didn't run that.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ramromconsultant Commented:
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
 
ashok3sepAuthor Commented:
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
 
ashok3sepAuthor Commented:
How can i test all the varaibles in a tuple for None or empty string
0
 
ashok3sepAuthor Commented:
after that how can i test the variables Induvidually
0
 
ramromconsultant Commented:
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
 
ashok3sepAuthor Commented:
thanks for your suggestions  ramrom

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
 
ramromconsultant Commented:
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
 
mish33Commented:
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
 
ramromconsultant Commented:
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
 
ashok3sepAuthor Commented:
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
 
mish33Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now