保存变量以供以后使用和验证

时间:2017-08-14 11:34:39

标签: python python-3.x variables

我需要变量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()

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您不应该练习在代码中使用相同的函数名和变量名。 要运行此代码,请更新程序,以使用 edit()函数将idnum作为参数传递。

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>