此代码的目的是让用户输入一个数字,然后程序搜索有效数字的数组。如果用户提交的号码是有效的,那么程序就会报告它。如果该号码为INVALID,则报告为此类。但是,我可能有一个布尔问题,因为无论输入的数字是有效还是无效,程序都会告诉我它无效。
#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()
答案 0 :(得分:1)
在这一行:
print ("Account number ", validArray[result], " is a valid number."
您缺少关闭print语句的括号。