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

2024-04-24 06:35:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: ofto代码younumberfordefedit
2条回答

您不应该练习在代码中使用相同的函数名和变量名。 要运行此代码,请更新程序,将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>

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

相关问题 更多 >