Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的程序报告我的条目无效,即使它是有效的_Python_Arrays - Fatal编程技术网

Python 我的程序报告我的条目无效,即使它是有效的

Python 我的程序报告我的条目无效,即使它是有效的,python,arrays,Python,Arrays,这段代码的目的是让用户输入一个数字,然后程序搜索一个有效数字数组。如果用户提交的编号有效,则程序会将其报告为有效。如果号码无效,则报告为无效号码。然而,我可能有一个布尔问题,因为无论输入的数字是有效的还是无效的,程序都会告诉我它是无效的 #main def main(): #initialize vars userNum = 0 result = 0 #array size SIZE = 18 #valid account numbers

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

#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()
#主
def main():
#初始化变量
userNum=0
结果=0
#数组大小
尺寸=18
#有效帐号
validArray=[565884514520125789512287775418451271302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581001]
#user num是要搜索的帐号
userNum=int(输入(“输入要搜索的7位帐号:”)
#传递给isValidAccount函数的userNum
结果=isValidAccount(validArray、大小、用户数)
如果(结果==-1):
打印(“帐号”,userNum,“不是有效的号码。”)
其他:
打印(“账号”,validArray[结果],“是有效号码。”)
#用于确保输入的数字有效的函数(值为#待搜索)
def isValidAccount(数组、大小、值):
#将标志设置为False
发现=错误
#数组大小
尺寸=18
#将索引设置为0位置
索引=0
#设定位置
位置=-1
#while循环,用于单步遍历数组以查找有效数字
而(未找到&索引<大小):
if(validArray[index]==value):#如果在数组中找到了用户提交的值
找到=真
位置=索引
其他:
发现=错误
返回位置
main()
在这一行:

    print ("Account number ", validArray[result], " is a valid number."

您缺少一个括号来关闭print语句。

在该函数上面的print中缺少一个右括号这是一个列表,不是数组,对吗?如果validArray中的值总是向后扫描,直到找到实际错误,为什么不执行
呢?我想您的意思是
validArray.index(userNum)
@C8H10N4O2