Python Forum
Filtering with IF Statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Filtering with IF Statement (/thread-22836.html)

Pages: 1 2


Filtering with IF Statement - Mike2607 - Nov-28-2019

I am working on some Homework and the assignment call for us to open a CSV and pull the info out into columns with fixed widths. Afterwards we need to filter the info with after a user inputs a selection. and this is where I am stuck. I cant seem to get the list of states to filter after the user select. Any help would be great the assignment is due on Sunday and I have been working on it all week thanks to this forum and many other I am were I am now.

import csv

def Location(State): 
    State = state 
    if (State in state): 
        return True
    else: 
        return False

titleLocation = "Location"
titleDisease = "Disease"
titleNumber = "Number"
titleYear = "Year"
print("%-25s %-15s %-15s %-15s" %(titleLocation, titleDisease, titleNumber, titleYear))

total = 0
with open("health-no-head-sample.csv", "r") as health:
    file_reader = csv.reader(health)


    for aline in file_reader:
        total += float(aline[3])
        number = aline[3]
        state = aline[2]
        disease = aline[0]
        year = aline[5]
        print("%-25s %-15s %-15s %-15s" %(state, disease, number, year))
    print("%-25s %-15s %-15s" %('', 'Total', total))
State = input('Select a state ')
if State == state:
    filtered = filter(Location, state)
    for state in filtered:
        print(state)



RE: Filtering with IF Statement - Larz60+ - Nov-28-2019

Quote:Afterwards we need to filter the info with after a user inputs a selection.
please post original assignment


RE: Filtering with IF Statement - Mike2607 - Nov-28-2019

1. Write each line as part of a table, include a header before the table, and a summary line at the end. Use a fixed width for each column (don’t try to find the largest width like you did in the previous unit). Not every field of the original line is used in the output. You will have to do some research about the .format() function to print the number of cases with a comma. If you can’t get the comma in the number column, move on and come back to that once you have more of the program written. The key is to have all the columns line up.

2. Use some if statements to add three filters to your program that let the user select exactly one state, disease and year to include in the report. Prompt the user to enter these values.

example

Enter state: Colorado
Enter disease: smallpox
Enter year: 1928
State Disease Number Year

COLORADO SMALLPOX 340 1928
Total 340

3. Change your program so that if the user just hits return for a prompt, the program includes all the data for that field. For example:
Enter state (Empty means all): Colorado
Enter disease (Empty means all):
Enter year (Empty means all): 1928
State Disease Number Year

COLORADO MEASLES 2,099 1928
COLORADO POLIO 71 1928
COLORADO SMALLPOX 340 1928
Total 2,510


RE: Filtering with IF Statement - Larz60+ - Nov-28-2019

Are you allowed to use any or all of the following?:
  • lists
  • dictionaries
  • f-string



RE: Filtering with IF Statement - Mike2607 - Nov-29-2019

we are allowed to use list, expressions, f-strings.


RE: Filtering with IF Statement - Larz60+ - Nov-29-2019

No dictionaries? that would make the entire process much simpler.


RE: Filtering with IF Statement - Mike2607 - Nov-29-2019

Sorry can you provide any hints or tips?


RE: Filtering with IF Statement - Larz60+ - Nov-29-2019

read all of the rows into a list
input all 4 of the conditions before checking anything.
for each item in list
if list_state == state and list_disease == disease and list_year == year
and list_State_Disease_Number_Year == State_Disease_Number_Year:
print results and tally totals
print totals.
I's also use fstrings for print statements, like:
print(f"\n{titleLocation:25} {titleDisease:15} {titleNumber:15} {titleYear:15}\n")
and
print(f"{state:25} {disease:15} {number:15} {year:15}")


RE: Filtering with IF Statement - perfringo - Nov-29-2019

In your code you have used csv.Reader. Does that mean that you can also use csv.DictReader?


RE: Filtering with IF Statement - Mike2607 - Nov-29-2019

I guess but I am noob do that mean dictionary? if so I guess I have access to it. I am just not sure what the dictionary will do for the program.