Please help, my code does not work

This is the place for queries that don't fit in any of the other categories.

Please help, my code does not work

Postby kyophan » Tue Apr 15, 2014 8:03 am

The program reads from a text file called "names.txt" in order to calculate the most common first letter of the names in the file and output that letter to the screen.

Here is what I have so far:
Code: Select all
import string

first_letters = ""

infile = open("names.txt", "r")

for line in infile:

whole_line = line.strip()

first_letter = line[0]

first_letter = first_letter.lower()

first_letters += whole_line[0]
infile.close()

max_count = 0

max_count_letter = 'a'

for letter in string.ascii_lowercase:

letter_count = len(infile)

if letter_count > max_count:

    max_count = letter_count

    max_count_letter = first_letter
print(max_count_letter)

I don't know where the problem is.
kyophan
 
Posts: 1
Joined: Tue Apr 15, 2014 8:00 am

Re: Please help, my code does not work

Postby stranac » Tue Apr 15, 2014 9:30 am

You need to describe the problem.
Do you get any errors from python?
Do you get results different from what you expected?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Please help, my code does not work

Postby sboden » Tue Apr 15, 2014 9:45 am

Well... if that's the indentation in your python file it's wrong... you do funky stuff in the first part (futile calculations), I don't even 'get' your last part.
Try the code below ;) ... So, 1 more homework assignment solved. Can I suggest picking up an introductory book on python.

Code: Select all
hash_table = {}

#  Read file, count first letters in hash table
with open("names.txt", "r") as infile:
    for line in infile:
        stripped_line = line.strip()
        if ( len(stripped_line) > 0 ):
            letter = stripped_line[0].lower()
            if letter in hash_table:
                hash_table[letter] += 1
            else:
                hash_table[letter] = 1

#  Iterate over hash table find biggest count
#  This may not be that pythonic
max_count = 0
max_letter = None
for (letter) in hash_table:
    count = hash_table[letter]
    if count > max_count:
        max_letter = letter
        max_count = count

print("Letter '%s' occurs %d times (max count)" % (max_letter, max_count))
sboden
 
Posts: 6
Joined: Mon Apr 14, 2014 8:22 pm

Re: Please help, my code does not work

Postby snippsat » Tue Apr 15, 2014 5:03 pm

stranac have asked question that should be answered.
It's ok code @sboden,but a little to much to much code ;)
hash_table is ok for other languges like Java/C++,but in Python please call it dictionary.

A shooter solution's,and collections Counter has a metod called most_common that fit well to the question.
Maybe to short to deliver as a homework assignment,you have to explain code to ;)
Code: Select all
from collections import Counter

with open('names.txt') as f_obj:
     count_letter = Counter(i.strip()[0] for i in f_obj).most_common(1)[0]
     print('Letter: "{}" is most common with a count of: {}'.format(count_letter[0], count_letter[1]))
User avatar
snippsat
 
Posts: 156
Joined: Thu Feb 21, 2013 12:04 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests