简体   繁体   English

为什么此Python代码引发错误?

[英]Why is this Python code raising an error?

When Im typing for example NICK and HASLO I have the following error: 当我输入例如NICK和HASLO时,出现以下错误:

Traceback (most recent call last):
File "G:/Wszystko/WONSZ/test.py", line 21, in <module>
    nick = nick()
TypeError: 'str' object is not callable

When I type nick and haslo it works but either not So what I should do to fix it? 当我输入nick和haslo时,它可以正常工作,但不能正常工作。 Here is the code: 这是代码:

x = int
y = int

def nick():
    print ("Podaj swoj nick: ",end=' ')
    nick = input()
    return nick

def haslo():
    print ("Podaj swoje haslo: ",end=' ')
    haslo = input()
    return haslo

print ("Witaj!!!")

while True:
    x = 0
    y = 0
    nick = nick()
    haslo = haslo()

    if nick == "nick":
        x += 1

    if haslo == "haslo":
        y += 1

    if x==0:
        print("Nick bledny")
        if y == 0:
            print("Haslo bledne")
    if x==1 and y==1:
         break

print("Zalogowano")

What can I do to fix this? 我该怎么做才能解决此问题?

Repeating variable and function names can be dangerous. 重复变量和函数名称可能很危险。 In your case, the code got messy, and Python can't properly tell if you are referring to a variable or a function. 在您的情况下,代码变得混乱,Python无法正确判断您是指变量还是函数。

Here, I changed the name of the variables. 在这里,我更改了变量的名称。 Not only it works, it's also easier to read: 它不仅有效,而且更易于阅读:

x = int
y = int

def nick():
    print ("Podaj swoj nick: ",end=' ')
    nickInput = input()
    return nickInput

def haslo():
    print ("Podaj swoje haslo: ",end=' ')
    hasloInput = input()
    return hasloInput

print ("Witaj!!!")

while True:
    x = 0
    y = 0
    nickVar = nick()
    hasloVar = haslo()

    if nickVar == "nick":
        x += 1

    if hasloVar == "haslo":
        y += 1

    if x==0:
        print("Nick bledny")
        if y == 0:
            print("Haslo bledne")
    if x==1 and y==1:
         break

print("Zalogowano")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM