2016-04-14 8 views
1

エラーは既に存在するStopIterationステートメントを要求し、コードの誤ったセクションに配置した可能性があります。私はこれに似たジェネレータの使用を見つけることができません。エラー:Stop Pythonのジェネレータでのイテレーションエラー

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

関連する問題