juuul635
juuul635

Reputation: 1

Problem with program that makes acronyms out of lines in a txt file

I want to create a program that creates capitalized acronyms out of terms given in a txt.file. The txt file has to be accessed through the sys.stdin option. This is how far i've gotten

import sys
def main():

    def make_acronym(stng):

        lst = "".join(e[0] for e in stng.split())

        oupt = lst.upper()
        return oupt

        oupt = oupt.upper()
        return oupt

    inpt1= []
    for line in sys.stdin:
        inpt1.append(line.strip())
    print(make_acronym(str(inpt1)))
    
main()

and when using this txt file:

access point
American National Standards Institute
Arithmetic logic unit
Automated Fingerprint Identification System
A Programming Language
Central Processing Unit
Compact disk
Complex instruction set computer
Control unit
Cyclic redundancy checks
Digital Subscriber Line
Digital versatile disk
direct memory acces
domain name system
file transfer protocol
Graphic Interchange Format
graphical user interface
Hard disk drive
Internet protocol
Internet mail access protocol
Joint Photographic Experts Group
Mobile Internet Devices
Motion Picture Experts Group
Operating System
Personal computer
Reduced instruction set computer
Simple mail transfer protocol
Tagged Image File Format
universal serial bus
wide area network

I get an output of:

[P'NSI'LU'FIS'PL'PU'D'ISC'U'RC'SL'VD'MA'NS'TP'IF'UI'DD'P'MAP'PEG'ID'PEG'S'C'ISC'MTP'IFF'SB'AN

All the first letters of the acronyms are missing, does anyone know why?

Also I would like my final output to be formatted like this:

The acronym of 'access point' is: AP.
The acronym of 'American National Standards Institute' is: ANSI.
The acronym of 'Arithmetic logic unit' is: ALU.

How can I do this?

Upvotes: 0

Views: 49

Answers (1)

Jack Deeth
Jack Deeth

Reputation: 3357

OK, you want to do these things:

  • go through the input line by line
  • break each line into words
  • join the first letter of each word
  • make uppercase

You've got the first bit:

for line in sys.stdin:

Breaking the line into words:

    words = line.split()

Getting the first letter of each word:

   first_letters = []
   for word in words:
       first_letter = word[0]
       first_letters.append(first_letter)

You can reduce this to a list comprehension:

    first_letters = [word[0] for word in words]

Join into an acronym:

    acronym = "".join(first_letters)

Make uppercase (could do this earlier):

    acronym = acronym.upper()

Putting it together.

for line in sys.stdin:
    words = line.split()
    first_letters = [word[0] for word in words]
    acronym = "".join(first_letters)
    acronym = acronym.upper()
    print(acronym)

But you can reduce this further:

for line in sys.stdin:
    print("".join([word[0].upper() for word in line.split()]))

But that's harder to read. So, can put it into a function - worthwhile even if you only use the code in one place, if it makes your code easier to think about:

def make_acronym(line):
    """Create acronym from first letter of each word"""
    return "".join([word[0].upper() for word in line.split()])

...
for line in sys.stdin:
    print(make_acronym(line))

Upvotes: 1

Related Questions