1

该错误要求一个 StopIteration 语句,该语句已经存在,我可能只是将它放在错误的代码部分中。我找不到与此类似的生成器的任何用途。错误:

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()
4

1 回答 1

1

您在iterator此处将 an 与列表元素进行比较:

iter1 == RoomDetails[-1]:

但是迭代器不会“成为”东西——它更像是一个工具而不是一个值。例如,列表的迭代器如下所示:

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

因此,除非您的其他对象是同一个迭代器,否则它将始终返回 False,因此永远不会引发 ValueError。尝试使该测试更简单一些,它应该可以工作。

此外,您还没有真正创建迭代器 - 您需要产生值以使您的函数成为生成器表达式。仅仅返回一个列表或简单的东西来实现你的目标可能值得吗?通常,迭代器会产生 area() 的返回值:

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

由于您只是调用area(),而不是对其进行迭代,因此无论如何您都不需要它成为迭代器。

于 2016-04-14T11:29:21.320 回答