Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StopIteration error with a generator in python

The error asks for a StopIteration statement, which already exists and I may have just placed it in the wrong section of code. I can't find any use of a generator that is similar to this. The error:

Traceback (most recent call last):
  File "W:\My Data Sources\My Documents\A level\Computer Science\Python\Tasks\Painting estimate copy wout gen.py", line 102, in <module>
    area()
  File "W:\My Data Sources\My Documents\A level\Computer Science\Python\Tasks\Painting estimate copy wout gen.py", line 71, in area
    sub2()
  File "W:\My Data Sources\My Documents\A level\Computer Science\Python\Tasks\Painting estimate copy wout gen.py", line 48, in sub2
    area()
  File "W:\My Data Sources\My Documents\A level\Computer Science\Python\Tasks\Painting estimate copy wout gen.py", line 67, in area
    print("Please enter the dimensions of each wall in your",next(iter1),"when prompted.")
StopIteration

RoomDetails = []
wallDimensions = []
counter = 0


def rooms():


    RoomNum = str(input("Please enter the name of the room you require  painting (e.g. 'lounge'): "))

    RoomDetails.append(RoomNum)

    inp1 = input("Have you entered all the rooms you need to decorate? Y or N?: ")

    if inp1 == 'y':
    print("")


    elif inp1 == 'Y':
        print("")

    elif inp1 == 'n':
        print("These are the rooms you have entered thus far: ", RoomDetails)
        rooms()

    elif inp1 == 'N':
        print("These are the rooms you have entered thus far: ", RoomDetails)
        rooms()


def sub():

    wallH = float(input("What's the hieght of this wall? (In meters): "))
    wallW = float(input("What's the width of this wall? (In meters): "))

    wallD = wallH * wallW

    wallDimensions.append(wallD)


def sub2():

    global counter

    var3 = input("Have you entered the dimensions of all the walls in this room that require painting? Y or N?")
    if var3 == 'y':
        area()
    elif var3 == 'Y':
        area()
    elif var3 == 'n':
        sub()
        sub2()
    elif var3 == 'N':
        sub()
        sub2()


global iter1
iter1 = iter(RoomDetails)

def area():
    global counter
    counter = counter + 1


    print("Please enter the dimensions of each wall in your",next(iter1),"when prompted.")

   sub()

   sub2()

    if counter < len(RoomDetails):
        area()
    elif iter1 == RoomDetails[-1]:
        raise StopIteration



def calc():

    var4 = float
    var4 = sum(wallDimensions)
    #£4.24 per square metre for painting
   var5 = float
   var5 = 4.24
   finalAmount = var4 * var5
   print("The total cost to paint",RoomDetails,"will be £",finalAmount)
   input("...")



   print("Welcome to the evaluation")


   CustNum = input("Please enter your customer number: ")

   DateEst = input("Please enter the date of your estimate: ")

   rooms()

   area()

    calc()
like image 840
M G. Avatar asked Jun 07 '26 20:06

M G.


1 Answers

You are comparing an iterator to a list element here:

iter1 == RoomDetails[-1]:

But the iterator won't 'be' the thing - it's more like a tool than a value. For example, an iterator of a list looks like this:

>>> iter([])
<listiterator object at 0x6ffffdaf090>

So unless your other object is the same iterator, it will always return False and so never raise the ValueError. Try making that test a little simpler and it should work.

Also, you haven't really created an iterator - you need to yield values to make your function a generator expression. It might be worth just returning a list or something simple to achieve your goal? Typically, an iterator would yield the return value of area():

if counter < len(RoomDetails):
    yield area()

And as you are simply calling area(), not iterating over it, you don't need it to be an iterator here anyway.

like image 199
srowland Avatar answered Jun 10 '26 11:06

srowland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!