I have a program which checks an outlook inbox for attachments from specific senders in a dictionary. Python - Outlook search emails within specific range only
That program works fine, and now I'm trying to expand on it by giving the user the ability to choose a specific sender from the dictionary, so as to only search their emails. This code is within the previous program.
The dictionary I have is senderDict = {sender@address.com : Sender Name}
To turn it into a menu of values to choose from I've used enumerate:
listSender = list(senderDict.values())
for count, item in enumerate(listSender,1):
print(count, item)
print("There are", len(listSender), "items.")
while True:
senderNum = int(input("Choose a sender (zero for any): "))
try:
if senderNum <= len(listSender) and senderNum >= 0:
#senderNum = int(input("Choose a sender (zero for any): "))
print("you chose", listSender[senderNum])
break
elif senderNum == 0:
print("Searching for any sender")
break
elif senderNum < 0:
print("Cannot choose a negative number.")
continue
except ValueError: #Not int
print("You did not enter a valid number (ValueError)")
input()
except IndexError: #outside range
print("You did not enter a valid number (IndexError)")
input()
The issue is that choosing zero will choose the zero index, instead of 'search any'. How do I make the list index match the enumerate values?
Also, entering a non-number or blank input crashes the program, so I'm not certain what exception covers that, I thought it fell under ValueError.
I'm not really sure if I even need to turn the dictionary into a list, but I had read that it was necessary for enumerate to work so I just went with it.