线性搜索循环

时间:2016-05-18 16:00:54

标签: loops for-loop search while-loop linear

def main():
    print("This program would ask you, the user, to type in a number between 1 and and the number you would provide; we will then tell you the index of which the number you have searched for.")
    max = eval(input("Type in the number you want to become the max: "))
    searchItem = eval(input("Please enter a number between 1 and your max to search for: "))
    found = False
    numList = [max]
    print(numList)                                                  
    while found == False:
        for i in range(len(numList)):
            if numList[i] == searchItem:
                print("Your number",searchItem,"was found at index",i)
                found = True
                break

            else:
                print("Your number was NOT found at index",i)
    if found == False:
        print("The number you are searching for is not in the list.")

if __name__ == "__main__":
main()

我试图让用户成为最大号码,然后询问用户在最大号码内找到的号码。一旦它开始搜索,就会有一个无限循环说这个数字在索引0中找不到。

0 个答案:

没有答案