对登录名的二进制搜索引发TypeError:无法将“int”对象隐式转换为str

2024-05-23 20:49:57 发布

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

这是我尝试搜索用户名时遇到的错误:

[['tom@gmail.com', 'Password1'], ['harry@gmail.com', 'Password2'], ['jake@gmail.com', 'Password3']]
Please enter a usernametom@gmail.com
Traceback (most recent call last):
  File "C:\Users\TomEllison22\Documents\Work\A Levels\Computing\guesspassword2 (2).py", line 88, in <module>
    login=login_user(MY_PASSWORD)
  File "C:\Users\TomEllison22\Documents\Work\A Levels\Computing\guesspassword2 (2).py", line 68, in login_user
    index = BinarySearch(MY_PASSWORD,username,Last,ItemSought)
  File "C:\Users\TomEllison22\Documents\Work\A Levels\Computing\TEST_BINARY.py", line 19, in BinarySearch
    Midpoint = (First + Last) // 2
TypeError: Can't convert 'int' object to str implicitly

如何让它正确搜索并找到我输入的用户名?你知道吗

def login_user(MY_PASSWORD):
    MY_PASSWORD = BubbleSort(MY_PASSWORD)
    while True:
        username = input("Please enter a username")
        ItemSought=username
        if validateEmail(username):
            print("Not a valid email address")
            continue
        Last = len(MY_PASSWORD)-1
        index = BinarySearch(MY_PASSWORD,username,Last,ItemSought)
        if index > -1:
            break
        else:
            print("User doesnt exist")
    while True:
        print()
        password = input("Password:")
        print()
        if LengthCheck(password,5):
            print("Password too short")
            continue
        else:
            print("Password correct length")


def BubbleSort(MY_PASSWORD):
    NoSwaps = 1
    N = len(MY_PASSWORD)
    while NoSwaps == 1:
        Count = 1
        NoSwaps = 0
        for Count in range(N-1):
            if MY_PASSWORD[Count] > MY_PASSWORD[Count+1]:
                temp = MY_PASSWORD[Count]
                MY_PASSWORD[Count] = MY_PASSWORD[Count+1]
                MY_PASSWORD[Count+1]=temp
                NoSwaps=1
    return MY_PASSWORD


def BinarySearch(List,First,Last,ItemSought):
    ItemFound = False
    SearchFailed = False
    Midpoint = (First + Last) // 2
    #print('midpoint',Midpoint)
    if List[Midpoint] == ItemSought:
        ItemFound = True
    else:
        if First >= Last:
            SearchFailed = True
        else:
            if List[Midpoint] < ItemSought:
                BinarySearch(List,Midpoint + 1,Last,ItemSought)
            else:
                BinarySearch(List,First,Midpoint-1,ItemSought)
    if SearchFailed:
        print("searchfailed")
    if ItemFound:
        print("itemfound")

if __name__ == "__main__":
    List=["Tom","Harry","Sam","Joe"]
    List = BubbleSort(List)
    ItemSought=(input("Enter a username"))
    First = 0
    Last = len(List)-1
    print(List)#,First,Last,ItemSought)
    BinarySearch(List,First,Last,ItemSought)

Tags: comifmycountusernamepasswordelsegmail
2条回答
index = BinarySearch(MY_PASSWORD,username,Last,ItemSought)

不太清楚为什么您似乎是二进制搜索字符串(或无序列表),但您已将第一个设置为字符串,它需要是一个数字。你知道吗

这是行不通的

(username + Last) // 2

同样值得一提的是,请不要将Python变量名大写。这让别人很难读懂。你知道吗

如stacktrace所示,您使用字符串而不是整数调用BinarySearch

def login_user(MY_PASSWORD):
    ...
    username = input("Please enter a username")
    ItemSought=username
    ...
    Last = len(MY_PASSWORD)-1
    index = BinarySearch(MY_PASSWORD,username,Last,ItemSought)

将用户名(字符串)传递给BinarySearch两次:一次作为username(作为First参数的参数),一次作为ItemSought。然后,在BinarySearch

    Midpoint = (First + Last) // 2

这将尝试添加一个字符串和一个数字,结果是在除法或赋值之前出现TypeError。你知道吗

PS:看来你正在试图在密码列表中搜索登录名。。。我不知道你想做什么,但那看起来肯定是个错误。你知道吗

相关问题 更多 >