簡體   English   中英

為什么這個python程序無法啟動?

[英]why won't this python program start?

我在教程論壇上復制了其他人編寫的python程序的源代碼,並對其進行了一些修改以滿足我自己的需求

我原本是想在python shell中運行的,但我想讓它在shell中運行,但是我需要將其保存到python IDLE並從那里運行

我正在使用python 3.2.3 IDLE btw這是我寫的東西:

def fibonacci(previous=0,current=1):
    n = int(input("Calculate fibonacci sequence value up to: "))
    if previous > current:
        previous,current = current, previous
    yield previous
    yield current
    while True:
        current,previous = previous+current,current
        yield current

    x = fibonacci()    
    for i in range(n):
        print(next(x))
fibonacci()

它不會運行,就像沒有錯誤彈出一樣,我只是得到箭頭:>>沒事。

該程序確實啟動並運行。 不幸的是,通過在函數中使用yield ,您使其成為生成器,並且該生成器僅在最后一行構造,而從未進行評估。

相反,您要使最后四行超出:

def fibonacci(previous=0,current=1):
    if previous > current:
        previous,current = current, previous
    yield previous
    yield current
    while True:
        current,previous = previous+current,current
        yield current

n = int(input("Calculate fibonacci sequence value up to: "))
x = fibonacci()    
for i in range(n):
    print(next(x))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM