Code not giving any result


### Names that reached number one

#Write a program that reads every file in the data set and identifies all of the names

#that were most popular in at least one year.

# Number of years that are going to be processed

FIRST_YEAR = 1900

LAST_YEAR = 2012

# empty list to store both boys and girls names

#create a function that takes 2 parameters,name of the file and updates the lists

def popular_name(f_name,name):

#empty lists

    boys = []

    girls = []

# open the file

    f_open = open(f_name,'r')

# read the file

    f_contents = f_open.read()

    # check which list to update

    if name == "boys":

# update boys list

        for line in f_contents:

            if line not in boys:

                boys.append(line)   

    elif name == "girls":

# update girls list

        for line in f_contents:

            if line not in girls:

                girls.append(line)

    

    return boys,girls

def main():

    # if name is boys

    for year in range(FIRST_YEAR,LAST_YEAR + 1):

        f_name = "C:/Users/simmy/Downloads/baby_names/BabyNames" + str(year) + " /_BoysNames.txt"

        name = "boys"

        print(popular_name(f_name,name))

    # if name is girls

    for year in range(FIRST_YEAR,LAST_YEAR + 1):

        f_name = "C:/Users/simmy/Downloads/baby_names/BabyNames" + str(year) + " /_BoysNames.txt"

        name = "girls"

        print(popular_name(f_name,name))

Given your code, there is nothing to provide output. You define two functions but they are never called.

ok sorry for that, now its telling me it can’t find file, so l could not get the files location using properties so l manually typed it in the code


### Names that reached number one

#Write a program that reads every file in the data set and identifies all of the names

#that were most popular in at least one year.

# Number of years that are going to be processed

FIRST_YEAR = 1900

LAST_YEAR = 2012

# empty list to store both boys and girls names

#create a function that takes 2 parameters,name of the file and updates the lists

def popular_name(f_name,name):

#empty lists

    boys = []

    girls = []

# open the file

    f_open = open(f_name,'r')

# read the file

    f_contents = f_open.read()

    # check which list to update

    if name == "boys":

# update boys list

        for line in f_contents:

            if line not in boys:

                boys.append(line)   

    elif name == "girls":

# update girls list

        for line in f_contents:

            if line not in girls:

                girls.append(line)

    

    return boys,girls

def main():

    # if name is boys

    for year in range(FIRST_YEAR,LAST_YEAR + 1):

        f_name = "C:/Users/simmy/Downloads/baby_names/BabyNames"+ "/" + str(year)+"_BoysNames.txt"

        name = "boys"

        print(popular_name(f_name,name))

    # if name is girls

    for year in range(FIRST_YEAR,LAST_YEAR + 1):

        f_name = "C:/Users/simmy/Downloads/baby_names/BabyNames" + "/" + str(year) +"_BoysNames.txt"

        name = "girls"

        print(popular_name(f_name,name))

print(main())

This is the updated code now, even the path had to be corrected a bit but still doesn’t work

GIven I don’t have your files it’s kinda difficult to help without more information. How exactly is it not working?

A couple things that seem strange are your methods of returning. In popular_name you create two lists (boys, girls), then you only add to one of them, and then return both of them. Is this what you want? It seems overly complicated. Unless it is needed as part of a bigger program I would only return one list, and actually only create one list.

Second you print a call to main, but main doesn’t return anything so the result will be None.

so like there are files from 1900 to 2012, containing baby names,separate files for boys and girls and the numbers per name, so for every file I am supposed to take the most popular name boy and girl for each year and store it in the boys and girls list

It is telling me it can’t find these files,

so l print the main function cause it calls popular names function

If it can’t find these files, chances are it’s because they don’t exist (at least not were you expect them to). I’d look carefully at the path. Perhaps start were your file is instead of the C drive.

Printing a function call still won’t do anything if the function doesn’t return anything, you’re better off just calling it.

so according to me the whole path is there, so if l start after C:, where should l start. downloads,baby_names or

so the files exist

Before the downloads folder, the folders are still “Users/simmy/”? The image shows a user name Simmy Sond, which I believe is supposed to be the same as the user folder, I could be wrong on that though.

I’d start the file path from wherever the program is.

I don’t get this, the path of the baby names file is different from where the program is

Interesting, to my knowledge Python should be able to start a path at the C drive, but perhaps I’m missing something.

You can use ./ to go back a folder.

where do l put that, not sure

./ moves you to the parent directory of the directory you are currently in.

Say your have a file structure:

project/
  main/
    main.py
  rss/
    logs.txt

When you run main.py you’ll be in the main directory. Say you want to use logs.txt you’re file path would look like this:

../rss/logs.txt

The first period takes you to the programs parent directory (main/), the second takes you to the parent directory of main/ (project/).

So like where do l write the ./

Thanks

If Python is installed using the Windows installer, straight from download to Open and Install, it will go into the ‘AppData/…/’ folder of the current user. There should be a shortcut on the taskbar, or at a least a shortcut in the Start menu that can be pinned to the taskbar. Right-click and Properties to see the path to the .exe.

In that folder is a folder called ‘Scripts’. That is where Python will save your files by default. Bookmark that folder, and then do everything in there. Save all your work to project or subject files/folders in that folder. You’ll never be searching for your programs/projects, and IDLE will keep track of the most recent files you had open. Easy peasy.

Of course, this is not PyCharm, but Python 3.x.x from python.org. Download the .msi version for 64-bit or 32-bit that matches your machine. It won’t matter if you install the 32-bit version on a 64-bit machine, but it will lack a lot of power and memory capabilities.

For now, one suggests leave PyCharm alone and install IDLE, just for the user experience. No bells and whistles, basic text editor, and plain Jane shell interface. A scratch start. And a place to save files, built in.


FTR

One has never worked in a production environment based workplace. To this day I always have an open IDLE shell running in the background. Just habit, maybe, but also all I need.

Point to any WSYWIG program that is common in a workplace with dozens and dozens of workstations. An office where everybody is coding is likely to have licensed server based software that all the workstations and laptops can request resources from, and only the minimal distribution files installed on each machine. Everyone will be using the same environment.

I’m not in that category, so can be content with a scratch install. Yeah, I have a few of the other environments installed, but none of my projects is so large as to need a file management system. Like I said, just having the Scripts folder in the parent folder of the .exe is enough. What’s more, I have Python installs that go back many years, including 2.7.2, and lots of files in their Scripts folder to look back on.

Sadly, though, I have installs on several computers of varying versions and who knows how many orphaned projects and files. Were I cloud based going back 10 to 20 years, it would be a slightly different story. There is no greater time waster of a day than looking for code that is not even on that machine.

Using GitHub or REPL.it or CodeBits is a way to self-publish the code you want to last, especially after you put in the time to make it shine. For sure keep a local copy for offline access, and date it. Searching for it becomes a tonne easier when you’re online.

Bottom line, become a religious user of IDLE for the next few months, and make use of the online repos as a way to make your successful projects public, or at least shareable. Create your code on your machine. Debug and perfect it, on your machine. When it’s ready to publish, then put it on repl or whichever repo you are fond of. I favor repl.it since I’m not much of a collaborator. I dabble and create code snippets, always tinkering on examples for learners.

1 Like

so l have brought the folder with the other .py files and its showing me this and I copied the file path from visual studio itself, l right clicked on the file and clicked copy path

Thanks