“str”对象在python中不可调用

2024-09-29 17:18:39 发布

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

我在password()函数中调用这个函数password(),希望它能重新开始。然后出现“str”对象错误。在

代码:

import time

def sleepFor(sleepForInt):
    time.sleep(sleepForInt / 1000)

def newScreen(): # adte it says
    for itemNew in range(26): 
        print("\n")

def logo(): #  ^~-
    newScreen()

    print("\t\t\t\tLeigh Studio")
    indent(6)

    return None

def indent(space): # adte it says
    for item in range(space):
        print("\n")

def password():
    userName = input("Enter registered UserName > ")
    user = userName

    indent(1)
    if userName == "leigh flix":
        confirm = input("Confirm UserName (y/n) > ")
        indent(2)

        if confirm == "y":
            password = input("\tenter password ) ")

            if password == "comics123":
                menu()

            else:
                print("Password is Incorrect")
                sleepFor(2000)
                password()
        else:
            password()
    else:
        print("No registered UserName as: " + user)
        sleepFor(2000)
        password()

def printID():
    print("C:/users/" + user)

def menu():
    response = input(printID())

    while response != "quit":

        if response == "time":
            time.ctime()

def main(): # main method
    logo()
    sleepFor(1200)
    newScreen()

    password()


# ___Runs program___
main()

错误: enter image description here

我不知道还有什么。哦,顺便说一句,这不是一个编译错误,而是一个运行时错误,如果我没有输入正确的密码(它是'comics123')。在

我要问的问题是,有没有一种不同的方法来调用password() 函数,它不会得到这个错误,或者一种重复询问用户密码是否错误的方法。提前谢谢。在


Tags: 函数inputiftimedef错误usernamepassword
3条回答

^{cd1>}调用失败,因为您错误地选择将标识符^{{cd2>}重写为本地变量(通过^{cd3>}语句!)所以隐藏与它一致的全局(函数)名称。使用本地变量的不同的标识符,例如^{{cd4>}(当然,使用^{cd5>}引用刚刚输入的字符串!-)你会没事的。

你的问题在于:

password = input("\tenter password ) ")

在这个范围内的password这行后面是这个字符串变量,而不是函数。只需将此变量重命名为其他变量即可解决此问题。

    if confirm == "y":
        password = input("\tenter password ) ")

您在这里定义了一个名为password的变量,它隐藏了在顶层定义的password()函数。

^{pr2}$

在这里,您尝试调用作为输入的字符串,就好像它是一个方法一样。

您需要做的是为字符串和函数使用不同的名称。我建议将函数调用为request_passwordinput_password

相关问题 更多 >

    热门问题