我的程序报告我的输入是无效的,即使它是有效的

2024-10-04 01:27:53 发布

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

此代码的目的是让用户输入一个数字,然后程序搜索一个有效数字数组。如果用户提交的数字是有效的,那么程序会这样报告它。如果号码无效,则按此报告。然而,我可能有一个布尔问题,因为无论输入的数字是有效的或无效的程序告诉我它是无效的

#main
def main():
    #initialize vars
    userNum = 0
    result = 0
    #array size
    SIZE = 18
    #valid account numbers
    validArray = [5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
              8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
              1005231, 6545231, 3852085, 7576651, 7881200, 4581001]

    #user num is the account number to be searched
    userNum = int (input ("Enter your 7 digit account number to be searched: "))

    #userNum passed to isValidAccount function
    result = isValidAccount (validArray, SIZE, userNum)
    if (result == -1):
        print ("Account number ", userNum," is not a valid number.")
    else:
        print ("Account number ", validArray[result], " is a valid number.")


#function for making sure number entered is valid (value is # to be searched)
def isValidAccount(array,size,value):
    #set flag as False
    found = False
    #array size
    size = 18
    #set index to 0 position
    index = 0
    #set position
    position = -1
    #while loop for stepping through array to find a valid number
    while (not found & index < size):
        if (validArray[index] == value):  #if the value submitted by the user is found in the array
            found = True
            position = index
        else:
            found = False
    return position


main()

Tags: thetonumbersizeindexisvalueposition