Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Filtering with IF Statement
#1
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)
Quote
#2
Quote:Afterwards we need to filter the info with after a user inputs a selection.
please post original assignment
Quote
#3
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


Attached Files
.csv   health-no-head-sample.csv (Size: 1.84 KB / Downloads: 14)
Quote
#4
Are you allowed to use any or all of the following?:
  • lists
  • dictionaries
  • f-string
Quote
#5
we are allowed to use list, expressions, f-strings.
Quote
#6
No dictionaries? that would make the entire process much simpler.
Quote
#7
Sorry can you provide any hints or tips?
Quote
#8
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}")
Quote
#9
In your code you have used csv.Reader. Does that mean that you can also use csv.DictReader?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#10
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.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)