Search code examples
pythongrok

Counting Characters in python grok


there is a task where i have to enter names and at the end print how many different characters and how many repeats.

'Your program should read in multiple lines of input, until a blank line is entered. Then it should print out how many unique characters were named, and how many were repeated.'

This was my result:

Character: The doctor
Character: Rose
Character: Rory
Character: Clara
Character: K-9
Character: The Master
Character: The Doctor
Character: Amy
Character:
You named 8 character(s)
You repeated 1 time(s)

This was my code:

count = []   
country = input('Character: ')
a = country.count(country)
b = 0
c = 0
while country:
 count.append(country)
  country = input('Character: ')
  if a == country:
    b = b + 1
  else:
    c = c + 1
c = c - b
count.sort()
print('You named',c,'character(s)')
print('You repeated',a,'time(s)')

Supposed to say:

Character: The Doctor
Character: Rose
Character: Rory
Character: Clara
Character: K-9
Character: The Master
Character: The Doctor
Character: Amy
Character:
You named 7 character(s).
You repeated 1 time(s).


Solution

  • country = input('Character: ')
    
    a = country.count(country)
    b = 0
    c = 0
    
    while country:
    
     count.append(country)
     country = input('Character: ')
     if country in count:
       b = b + 1
     else:
       c = c + 1
    
    count.sort()
    print('You named',c,'character(s)')
    print('You repeated',b,'time(s)')
    

    Result:

    Character: "AA"
    Character: "BB"
    Character: "CC"
    Character: "BB"
    Character: ""
    ('You named', 3, 'character(s)')
    ('You repeated', 1, 'time(s)')

    Key changes:

     if country in count:     //changed
    

       c = c - b             //removed
    

    print('You named',c,'character(s)')        //changed
    print('You repeated',b,'time(s)')          //changed