Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 缩进错误取消缩进与任何外部缩进级别不匹配_Python 3.x_String - Fatal编程技术网

Python 3.x 缩进错误取消缩进与任何外部缩进级别不匹配

Python 3.x 缩进错误取消缩进与任何外部缩进级别不匹配,python-3.x,string,Python 3.x,String,我是python的初学者 我编写代码来接收一个字符串并找到它最常出现的字符 userInput=input() my_input=list(userInput) count= {} for num in my_input: count[num] = count.get(num,0)+1 m=max(count) print(m) 当我执行时,我收到这个错误 File "third.py", line 8 m=max(count)

我是python的初学者

我编写代码来接收一个字符串并找到它最常出现的字符

userInput=input()
my_input=list(userInput)

count= {}

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

print(m) 
当我执行时,我收到这个错误

File "third.py", line 8
    m=max(count)
               ^
IndentationError: unindent does not match any outer indentation level

通常,这些错误位于错误中显示的内容之前的行中。我可以很容易地看到你的
count[num]
距离右边太远了一个空格。我认为Python中的缩进通常距离左边距4个空格

根据您的文本编辑器,您还可以通过删除
for
循环中行前的空格来修复它,即

for num in my_inputs:
count[num] = count.get(num, 0)+1
m=max(count)
然后按
选项卡
键对其进行格式化

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

所以现在发生的是,你不均匀的间隔会把它扔掉。尝试使用4个空格或1个制表符(但确保您的IDE可以将其转换为空格)。

感谢您的澄清,我更正了它。当我输入字符串“aaaaaaaaaaaaaaaaaaaaaaaabbbbcddddeeeee”时,我还有一个问题,它会给我输出“e”而不是“a”。我对此有点困惑,这是因为当您这样做时,
max(count)
它取的是键的最大值,而不是键中的值。在Python中,“e”大于“a”,因此
max(count)
返回“e”。要获取具有最大值的密钥,可以执行
max(count,key=count.get)
,该操作基于中的答案
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)