0
numbers = input("Enter the numbers: ")
count = {}
for i in numbers:
    if i == '-100':
        break
        print('Done')
    elif i in count:
        count[i] +=1
    else:
        count[i] = 1
        
for key,value in count.items():
    print(f'{key}:{value}')


I just don't understand how to use the 'break' to stop the loop. Can anyone helps me to fix that? thanks

  • What you have will do that, but remember that the `break` exits the loop IMMEDIATELY, so the `print` won't run. You'd need that before the break. The bigger problem is that `numbers` is just going to be one big string. How are they going to enter the numbers? – Tim Roberts Oct 26 '22 at 06:33

1 Answers1

0

you should use while loop for your case like this

count = {}
while True:
    numbers = input("Enter the numbers: ")
    if numbers == '-100':
        print('Done')
        break
    
    count[numbers] = count.setdefault(numbers, 0) + 1

for key,value in count.items():
    print(f'{key}:{value}')
Deepak Tripathi
  • 3,175
  • 1
  • 8
  • 21