2017-06-10 29 views
0

我不是程序員,但我打算這樣做,所以我正在學習python。 因此,在這段代碼中,我想創建一個計時器,但是當我啓動它時,python給了我一個for循環的「:」的無效語法。但是,如果我刪除它們,python突出顯示了它下面的n(在n = n + 1中)並給了我相同的錯誤。 什麼問題?在一個簡單的python程序中無效的語法

import time 

def step(): 
    time.sleep(1) 

n=0 
x=int(input("How many seconds? ") 
for n in range(0,x): 
    n=n+1 
    return n 
    step() 
+1

不要在循環返回(這將是你的下一個問題) –

+1

在'x = int(輸入(「多少秒?」)缺少閉括號'行 – pramod

+0

對於一個好問題沒有太多的材料... –

回答

0

你缺少一個額外結束括號

x=int(input("How many seconds? ") 
           ^
           here 
1

你缺少一個右paranthesis爲int

x=int(input("How many seconds? ")) # you only had one) 

請注意,你可能要print(n)內環路。該return將立刻退出循環:

import time 

def step(): 
    time.sleep(1) 

# No need to do "n=0"! 
x=int(input("How many seconds? ") 
for n in range(0,x): 
    n = n + 1 
    print(n) 
    step() 

也沒有必要做n = n + 1你可以簡單地調整range(從1x+1):

x=int(input("How many seconds? ") 
for n in range(1, x+1): 
    print(n) 
    step()