2015-01-26 11 views
0
import ui 
from time import * 
start = int(time()) 
def stop_time(sender): 
    finish = int(time()) 
    total_time = int(finish - start) 
    button1 = str("Your time is %i seconds." % (total_time)) 
    sender.title = None 
    sender.title = str(button1) 

어떻게하면 시작 변수를 변경할 수 있습니까? 당신이 함수에 처음 식별자를 할당 할 때 기본적으로

def restart_time(sender): 
    start = int(time()) 
    button2 = str("Stopwatch restarted.") 
    sender.title = None 
    sender.title = str(button2) 
ui.load_view('stop_time').present('sheet') 

답변

1

, 그것은 같은 이름의 세계 하나이 경우에도, 로컬 변수를 만듭니다. 이 시도 :

def restart_time(sender): 
    global start 
    start = int(time()) 
    button2 = str("Stopwatch restarted.") 
    sender.title = None 
    sender.title = str(button2) 

the relevant entry in the Python FAQ에서 : 파이썬에서

을, 단지 함수 내에서 참조되는 변수는 암시 적으로 글로벌 이다. 변수에 함수의 본문 내에있는 새 값 이 할당되면 해당 변수는 로컬로 간주됩니다. 변수 에 함수 내에서 새 값이 할당되면 변수는 암시 적으로 로컬 인 이고 명시 적으로 '전역'으로 선언해야합니다.

관련 문제