-1

I have the following code:

shape = input("Please enter your choice of shape? ")    

nthTime = ["second","third","fourth","fifth","sixth"]

while shape.lower() not in ["octagon","heptagon","hexagon"] :
    print("Please select a shape from the list!")
    shape = input("Pick a shape, for the " + nthTime + " time! ")

How can I achieve the outcome, that Python will iterate over the list 'nthTime', each time it passes through the `while-loop?

I could use a nested for-loop but of course this would run the whole list, which is not my aim.

Therefore, I conclude that I would need to use a nested while-loop; however I cannot figure out the exact syntax.

I'm hoping this will be a useful question for others also in the future.

Andrew Hardiman
  • 929
  • 1
  • 15
  • 30
  • A nested for loop under the while over `nthTime` **will** *iterate over the list each time it passes through the while-loop*. What is your problem? – OneCricketeer Jan 02 '16 at 18:15
  • may i guess you want something like `shape = input("Pick a shape, for the " + nthTime[iteration_index] + " time! ")`? – Netwave Jan 02 '16 at 18:16

3 Answers3

2

Use a for-loop and break out of the loop only if the user enters the correct value

for iteration in nthTime:
    if shape.lower() not in ["octagon","heptagon","hexagon"]:
        print("Please select a shape from the list!")
        shape = input("Pick a shape, for the " + iteration + " time! ")
    else: break
else: print "You have wasted all your chances, try again later"
smac89
  • 39,374
  • 15
  • 132
  • 179
1

Something like this:

shape = input("Please enter your choice of shape? ")    

nthTime = ["second","third","fourth","fifth","sixth"]
undesired_shapes = ["octagon","heptagon","hexagon"]

indx = 0

while shape.lower() not in undesired_shapes:
    print("Please select a shape from the list!")
    shape = input("Pick a shape, for the " + nthTime[indx] + " time! ")
    indx += 1
    if indx >= len(nthTime):
        print 'Giving up !!'
        break
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
0
nthTime.reverse()

while shape.lower() not in ["octagon","heptagon","hexagon"] and nthTime:
    print("Please select a shape from the list!")
    shape = input("Pick a shape, for the " + nthTime.pop() + " time! ")
donopj2
  • 3,933
  • 5
  • 23
  • 30