2016-04-14 42 views
1

錯誤要求提供StopIteration語句,該語句已存在,我可能只是將它放在錯誤的代碼段中。我找不到任何類似於此的發電機。錯誤:Python中的生成器的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() 

回答

1

您比較的iterator到一個列表元素在這裏:

iter1 == RoomDetails[-1]: 

但迭代器不會「是」的東西 - 它更像是一個工具比值。例如,一個列表的迭代器看起來是這樣的:

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

所以,除非你的其他對象是相同的迭代器,它總是返回False,因此永遠不會提高ValueError異常。試着讓測試更簡單一點,它應該可以工作。另外,您還沒有真正創建迭代器 - 您需要生成值以使您的函數成爲生成器表達式。爲了實現您的目標,您可能需要返回一個列表或簡單的東西。通常情況下,一個迭代器將產生區(返回值):

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

而且因爲你是簡單地調用area(),不遍歷它,你不需要它是這裏的迭代器呢。

+0

太好了,謝謝!我對編程相當陌生,無論是我還是我的計算機科學老師都不知道我錯在哪裏。對不起,如果它看起來像一個愚蠢的問題:) –

+0

非常高興提供幫助。如果它解決了您的問題,請隨時接受答案:)。謝謝。 – srowland