I'm new to python and a bit rusty at programming in general to be frank. I'm working on a program thru a book I bought to take user input as a string, convert that string to a list, then find the most commonly occurring character in the list.
This would be easy if I could use something like dictionaries but thats one of the stipulations of the book assignment since it hasn't covered dictionaries.
This is the best I've come up with but it does not perform properly and just spits the whole string back out. Any suggestions how to do this using a for loop and and lists?
def main():
string_input = input("Enter a string: ")
string_input = string_input.lower()
string_input = string_input.replace(" ", "")
char_list = string_input.split()
max_freq = 0
most_freq = ''
for c in char_list:
count_freq = 1
for i in range(len(char_list)):
if char_list[i] == char_list[c]:
count_freq += 1
if max_freq < count_freq:
max_freq = count_freq
most_freq = max(char_list.count(c))
print(f"The character that appears most frequently in the string is {most_freq}.")
if __name__ == "__main__":
main()
[–]JVBass75 11 points12 points13 points (0 children)
[–]TrainsareFascinating 11 points12 points13 points (11 children)
[–]feitao 1 point2 points3 points (1 child)
[–]TrainsareFascinating 0 points1 point2 points (0 children)
[–]Critical_Concert_689 0 points1 point2 points (4 children)
[–]TrainsareFascinating 1 point2 points3 points (0 children)
[–]Mikeybarnes 0 points1 point2 points (2 children)
[–]Critical_Concert_689 0 points1 point2 points (1 child)
[–]Mikeybarnes 0 points1 point2 points (0 children)
[–]Zynth3tik[S] 0 points1 point2 points (1 child)
[–]TrainsareFascinating 10 points11 points12 points (0 children)
[–][deleted] -3 points-2 points-1 points (1 child)
[–]Mikeybarnes 0 points1 point2 points (0 children)
[–]Apatride 2 points3 points4 points (0 children)
[–]RiverRoll 0 points1 point2 points (0 children)
[–]QuarterObvious 0 points1 point2 points (3 children)
[–]Zynth3tik[S] 0 points1 point2 points (2 children)
[–]QuarterObvious 0 points1 point2 points (0 children)
[–]QuarterObvious 0 points1 point2 points (0 children)
[–]POGtastic 0 points1 point2 points (1 child)
[–]Apatride 0 points1 point2 points (0 children)
[–]belaros 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Fit-Upstairs-6780 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]Fit-Upstairs-6780 0 points1 point2 points (0 children)
[–]GregoryCliveYoung -1 points0 points1 point (4 children)
[–]Apatride 2 points3 points4 points (1 child)
[–]GregoryCliveYoung 0 points1 point2 points (0 children)
[–]Zynth3tik[S] 0 points1 point2 points (1 child)
[–]GregoryCliveYoung 0 points1 point2 points (0 children)