Using the following code I am trying to check the users input string (CAcodes) and checking each one individually by assigning each element in CAcodes to a list (CAcode). A for-loop checks the conditions to make sure that the conditions for each code are met. The main for-loop tests several things.
1.) Try Statement: tests if there is a url entered for the corresponding code in the dictionary (urldict). If there is no url entered for that specific code, then the dictionary holds the value to be "0.0". If the value is "0.0" (making k =1) the program breaks at a later 'if' statement and prints that there is no url entered for that code.
2.) Except KeyError: tests each code the user inputed is in the urldictionary. If the code is not found (making j=1), the program prints that the code was not found and then an 'elif' statement breaks (if j == 1:).
If there are no problems (j and k both = 0 and the length of the list = y), then the for-loop breaks and then breaks the while loop. Then prints the desired information (print courseprint(CAcode)).
The Problem: The for-loop only tests for the first code in the list. If both codes are valid and have entered url's then the program runs fine. It also runs fine if the first code is not valid or has no url. But if the subsequent code is not valid, the program prints the desired information for the first code and then crashes on the second.
while True:
y = 0
CAcode = []
CAcodes = str(raw_input("\nEnter CA Codes (separated by a space): "))
CAcodes = CAcodes.split()
for codeA in CAcodes:
CAcode.append(int(codeA))
for codeB in CAcode:
print codeB
j = 0
k = 0
y += 1
try:
if urldict[int(codeB)] == "0.0":
print "A"
k = 1
break
else:
print "B"
break
except KeyError:
print "C"
j = 1
print "\nSorry, " + str(codeB) + " was not found. Please enter a valid CA code."
if j == 1:
print "D"
break
elif k == 1:
print "E"
print"\nError: There is no URL entered for " + str(codeB) + "."
break
elif (y == len(CAcode)) and (j != 1) and (k != 1):
break
if (y == len(CAcode)):
break
print ""
print courseprint(CAcode)
Thanks in advance!