2017-08-14 65 views
-3

我需要可以從多個函數調用變量idnum(讓用戶輸入數字,程序會在需要時檢索它)。我想下面的代碼,但出現該錯誤消息:保存變量供以後使用和驗證

TypeError: unorderable types: function() > int() 

什麼需要改變,以允許工作代碼和驗證輸入。

idnum= "" 

def idnum(): 
    idnum = int(input("Enter the id number of who you want to edit: ")) 
    edit() 


def again(): 
    edit() 

def edit_info(): 
     print() 
     print() 
     print ("Select what you want to edit") 
     edit_menu() 

def edit(): 
    num_lines = sum(1 for line in open('Surname')) 
    print() 
    if idnum > num_lines or idnum ==0 or idnum < 0: 
     print("Not valid") 
     time.sleep(0.5) 
     print("Try again") 
     time.sleep(0.2) 
     again() 
    else: 
     print() 
     for file in ["Forename", "Surname", "Email", "Date of birth", "Home address", "Home phone number", "Gender", "Tutor group"]: 
      with open(file) as f: 
       print(f.readlines()[idnum-1], end='') 

IDNUM()

+0

你不能有一個同名的函數和變量。您將'idnum'分配爲字符串變量,然後定義一個名爲'idnum'的函數。後來,當使用'idnum'時,代碼引用'idnum'函數,而不是字符串變量,這是錯誤的直接原因。 – vealkind

回答

0

嘗試global idnum = idnum = int(input("Enter the id number of who you want to edit: "))

+0

試着解釋爲什麼會有幫助... – jwenting

0

你不應該在練習你的代碼有相同的函數名和變量名。 運行此代碼更新您的程序以傳遞idnum作爲參數edit()函數。

def idnum(): 
    idnum = int(input("Enter the id number of who you want to edit: ")) 
    edit(idnum) 

def edit(idnum): 
    num_lines = sum(1 for line in open('Surname')) 
    print() 
    if idnum > num_lines or idnum ==0 or idnum < 0: 
    <Rest Code>