1

I'm a beginner in python.

I code to receive a string and find it's most occurring character

userInput=input()
my_input=list(userInput)

count= {}

for num in my_input:
     count[num] = count.get(num,0)+1
    m=max(count)

print(m) 

when i executed i receive this error

File "third.py", line 8
    m=max(count)
               ^
IndentationError: unindent does not match any outer indentation level
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Asha
  • 805
  • 3
  • 10
  • 18
  • 1
    Does this answer your question? [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) – Karl Knechtel Jul 14 '23 at 19:29

2 Answers2

1

Typically, these errors are in the line before what it shown in the error. And I can easily see that your count[num] is one space too far to the right. I think that indentation in Python is typically 4 spaces from the left margin.

Depending on your text editor, you could also fix it by deleting the spaces before the lines in the for loop, i.e.

for num in my_inputs:
count[num] = count.get(num, 0)+1
m=max(count)

and then pressing the tab key to format them.

for num in my_inputs:
    count[num] = count.get(num, 0)+1
    m=max(count)
m13op22
  • 2,168
  • 2
  • 16
  • 35
0
userInput=input()
my_input=list(userInput)

count= {}

for num in my_input:
     count[num] = count.get(num,0)+1
     ^ this is at the 5th blank space indent    
    m=max(count)
    ^ this is at the 4th space indent (the correct one)
print(m) 

so whats happening is its throwing it off with your uneven spacing. Try to stick to 4 spaces or just 1-tab(but make sure your IDE can convert it to spaces).

William Bright
  • 535
  • 3
  • 7
  • Thanks for the clarification, i corrected it.I have one more question when i input string 'aaaaaaaaaaaaaaaaaabbbbcddddeeeeee' it gives me the output 'e' not 'a'.I'm little bit confused in it – Asha Feb 24 '19 at 18:09
  • 1
    That's because when you do `max(count)` it's taking the maximum value of the keys, not the values in the keys. In Python, 'e' is greater than 'a', so `max(count)` returns 'e'. To get the key with the maximum value, you can do `max(count, key=count.get)`, which is based on the answer in [this question](https://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary) – m13op22 Feb 24 '19 at 18:21