I want to print out a dictionary’s contents after the user inputs ctrl-d, and while the program handles the exception for the EOFError, it doesn’t carry out the code inside the exception.
my code is:
grocery_list = {}
while True:
try:
item = input("")
for product in grocery_list:
if item.strip().lower() == product:
grocery_list += 1
else:
grocery_list[item.strip().lower()] = 1
except EOFError:
for product in grocery_list:
print(f"{grocery_list} {product.upper()}")
exit()
this is the code i particularly need executed
for product in grocery_list:
print(f"{grocery_list} {product.upper()}")
i expected something like:
1 APPLE
2 BANANA
1 SUGAR
once the user inputs ctrl-d if the user entered apple once, banana twice, and sugar once prior.
but upon hitting ctrl-d the program just ends
strangely enough, when i just print within the exception
except EOFError:
print("x")
exit()
it does carry out and prints
x
could anyone explain?
New contributor