为什么这个Python代码会引发错误?

2024-09-24 22:33:09 发布

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

例如,当我键入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

当我输入nick和haslo时,它工作了,但不是 那我该怎么做才能解决呢? 代码如下:

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")

我能做些什么来解决这个问题?你知道吗


Tags: input键入returnifdef错误nickint
1条回答
网友
1楼 · 发布于 2024-09-24 22:33:09

重复变量和函数名可能很危险。在您的例子中,代码变得凌乱,Python无法正确地判断您引用的是变量还是函数。你知道吗

在这里,我更改了变量的名称。不仅有效,而且更容易阅读:

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")

相关问题 更多 >