0

I'm nearly done building a dictionary that counts the number of each element entered...Although for example if I input coffee water and then enter, prints 1 coffee water in the same line... I want it to print:

1 coffee
1 water

in separate lines

What am I doing wrong?

dictionary = {}  

while True: 
    user_input = input("Input: ")

dictionary[user_input] = dictionary.get(user_input, 0) + 1

for key, value in sorted(dictionary.items()):
    print(value, key.upper())
    print("\n")

3 Answers3

2

Okay, here's the thing.

input() ends when Enter is hit. i.e. - if you type "coffee water" and then press enter, it's gonna think that's the name of the item you're entering. ("coffee water")

Basically, enter one item at a time.

Or, if you want, split by whitespace and support the addition of multiple items at the same time. something like:

dictionary = {}

value = input("Enter item: ")

while value !="":   
    value = value.split(" ") # split by space.
    # if there's no space (i.e. only one item is entered this time, this gives a list with a single item in it. (the word entered)
    for item in value:
        if item in dictionary.keys(): # if the item exists, add 1 to its counter
            dictionary[item] +=1
        else: # if the item doesn't exist, set its counter to 1
            dictionary[item] = 1
    value = input("Enter item: ")


for key, value in sorted(dictionary.items()):
    print(value, key.upper())

Entering:

coffee
water
water coffee 

gives:

2 COFFEE
2 WATER

Note: this breaks if you have items with spaces in their name. like "water bottle"

also read about the defaultdict module

Yarin_007
  • 1,449
  • 1
  • 10
  • 17
0

To solve the problem you mention in your last comment and to provide an escape from the while loop, I would revise your code as follows. This changes lines 2, 5, 6, 8 and 9. I had trouble getting my code in here. Let me know if you have any problems.

dictionary = {}  
i = 1
while True: 
    user_input = input("Input: ")   
    if user_input == "":
        break #provides escape   
    dictionary[user_input] =  dictionary.get(user_input, i) 
    i = i + 1 #solves numbering problem

for key, value in (dictionary.items()):
   print(f"{value} {key.upper()}", end=" ")   
   print("\n")
gerald
  • 420
  • 2
  • 8
  • Doesn't work as expected...If i type Coffee Coffee in same input and then enter prints 1 COFFEE COFFEE... Should print 2 COFFEE...Also tested if i input Coffee then enter and then Water and enter...prints 1 COFFEE 2 WATER...should print 1 COFFEE 1 WATER in separate lines...only when is repetitive the number should change... –  May 04 '22 at 23:47
  • Sorry, I misunderstood the problem, but it looks like Yarin_007 nailed it since then. – gerald May 05 '22 at 05:45
0

I don’t have my PC in front of me so this is kind of pseudo code.

Try something like:

input_list = user_input.split()

for i in input_list:
    dictionary[i] = dictionary.get(i, 0) + 1
    print(i)

By default the input will be as one string, so you cannot do your operations on it directly, you need to split it.

Edit: check out Yarin_007’s answer, however you can use the default .split() instead so that it gets split on any white space.

D.Manasreh
  • 900
  • 1
  • 5
  • 9