2016-11-25 219 views
1

我有兩個函數中的返回值。請參閱下面的功能;使用其他功能

def check_channel_number(self): 

    print "***************Channel Checker *********************" 

    print '' 

    user_channel_number = int(re.sub('\D', '', raw_input("Enter a channel number, (3digit): "))[:3]); 

    channel = ("channelNr= '%d'") % (user_channel_number) 

    print channel 

    # channel_search = channel + str(user_channel_number) 

    datafile = file('output.txt') 

    found = False 

    for line in datafile: 

     if channel in line: 

      found = True 
      print 'The channel number you entered is correct and will be deleted' 
      return user_channel_number 

    print 'The channel number you entered is not on the planner' 
    return False 

只是想在下面的函數中使用(user_channel_number)的返回值。

def delete_events(self): 



    if user_channel_number == True: 

     return 'The program number is correct and will be deleted' 

     # action = 'DeleteEvent' 
     menu_action = 'all' 
     book = 'RECYC:687869882' 
     arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed' 
         '\\Utils\\UPNP_Client_Cmd_Line.py')] 
     arg_list.append(' --action=') 
     arg_list.append(action) 
     arg_list.append(' --ip=') 
     arg_list.append('10.10.8.89') 
     arg_list.append(' --objectId=') 
     arg_list.append(book) 

     subprocess.call(["python", arg_list]) 

     print 'The program deleted successfully' 

當我運行我的腳本時,它說user_channel_number沒有全局定義?如何在delete_events()函數中調用user_channel_number

+0

1.類型強制轉換'user_channel_number'到'int',然後嘗試把它比作'真'。 2.「check_channel_number」總是返回false。 3.您的'delete_events'中的return語句會在運行之後停止所有代碼。看起來你在上課?您可以定義'self.user_channel_number'使其可以訪問您所在類中的所有方法。 – Signal

回答

2

函數不能分享他們的局部變量。您可以從第一個返回值,並將它傳遞給第二:

def check_channel_number(self): 
    ... 
    return user_channel_number 

def delete_events(self): 
    user_channel_number = self.check_channel_number() 
    ... 

或對象的保存價值:

def check_channel_number(self): 
    ... 
    self.user_channel_number = user_channel_number 
    ... 

def delete_events(self): 
    if self.user_channel_number: 
     .... 
1

所以我認爲,當你調用check_channel_number功能,user_channel_number的定義在那裏,所以當你打電話給delete_events,它已經超出了範圍,也許這樣的事情會有所幫助?

user_channel_number = check_channel_number() 
delete_events() 

我可能會擁有user_channel_number作爲輸入刪除功能太多,所以它會變成這樣:(其中UCN是user_channel_number

def delete_events(self, ucn): 
    if ucn == True: 
     print 'The program number is correct and will be deleted' 

     # action = 'DeleteEvent' 
     menu_action = 'all' 
     book = 'RECYC:687869882' 
     arg_list = [('C:\\Users\\yke01\\Documents\\StormTest\\Scripts\\Completed' 
        '\\Utils\\UPNP_Client_Cmd_Line.py')] 
     arg_list.append(' --action=') 
     arg_list.append(action) 
     arg_list.append(' --ip=') 
     arg_list.append('10.10.8.89') 
     arg_list.append(' --objectId=') 
     arg_list.append(book) 

     subprocess.call(["python", arg_list]) 

     print 'The program deleted successfully' 

我也改變了`回報'程序編號是正確的,將被刪除''到一個打印語句,因爲我有一種感覺,返回會在其他代碼行運行之前結束函數

因此,代碼可能最終會變成類似:

user_channel_number = check_channel_number() 
delete_events(user_channel_number) 

編輯:

只注意到它看起來像你的函數是一個類的一部分,

在這種情況下,你可以這樣做:

self.ucn = self.check_channel_number() 
self.delete_events(self.ucn) 

(或者,如果你不想到user_channel_number傳遞到功能,你可以改變if user_channel_number:if self. user_channel_number:

3

當你在一個函數中定義一個變量時,它是一個局部變量,這意味着它只能在該函數中被訪問。

中的類

看起來你是一個類裏面,這樣你就可以通過定義它像這樣使變量訪問類中的所有方法:

def check_channel_number(self): 

    self.user_channel_number = ... 

而且然後在你的第二個功能,你可以用它像下面這樣:

def delete_events(self): 

    if self.user_channel_number: 

一類

外面如果不使用類的內部方法,你可以改用global內置。

例如,

def check_channel_number(): 

    global user_channel_number 
    user_channel_number = ... 

def delete_events(): 

    if user_channel_number: 

     ... 

使用而不是在你的第一個功能,check_channel_number()從功能

返回一個值,你可以把它返回user_channel_number。然後,您可以調用該函數的delete_events()內,如下所示:

def check_channel_number(): 

    user_channel_number = ... 
    return user_channel_number 

def delete_events(): 

    if check_channel_number(): 

     ...