2017-07-16 30 views
0

我有一個非常簡單的測試設置在一個名爲test1.py的文件,但它似乎給我一個冒號預期的錯誤,或者當我運行它說無效的語法。 類測試1:冒號預計在Python

def counter(self): 
     while loopcount < 1000: 
      loopcount = loopcount + 1 
     if loopcount 1000: 
      print(loopcount) 
+0

什麼是'如果loopcount 1000:'怎麼辦呢? – jwodder

+1

'if loopcount == 1000:' – frozen

回答

1

讓我們試試這個代碼進行一些調整上loopcountif聲明和test1類定義

class Test1(object): 

    def counter(self, loopcount): 
     while loopcount < 1000: 
      loopcount = loopcount + 1 
     if loopcount == 1000: 
      print(loopcount) 

my = Test1() 
my.counter(100) 
0

你錯過了 '==' 在聲明中,if loopcount 1000:

現在,如果我理解正確,您希望打印1000如果loopcount <= 1000您可以通過以下方式更直觀地執行此操作,

def counter(self): 
    if loopcount <= 1000: 
     print 1000 

如果您還需要更新的loopcount值,跳過循環,並做了以下方式,

def counter(self): 
    loopcount = max(loopcount, 1000) 
    if loopcount == 1000: 
     print(loopcount)