Skip to content
Sign upLog in
← Back to Community
Hunting License project
Profile icon
NicJKincheloe

I am trying to write a program where the user can look up a county(I live in Virginia) and look up a certain hunting license. I want it to not only print the numbers of licenses purchased in each county but also find the difference for that license between 2019 and 2020. I got my data from two EXCEL sheets off a Virginia hunting website and copy-pasted the data into two files. So if I look up Fairfax County, and then resident archery, it should print the number of resident archery licenses in both 2019, and 2020, then print the difference between the two years. My biggest problem right now is figuring out how to pull the data from the file.I tried using functions but I don't know what to do next. (Edit: I have since changed it to just do a single county instead doing the entire state)

Comments
hotnewtop
Profile icon
yagode2674

Cremation has become an extremely popular alternative to a traditional burial. Cremation supplies include corrugated containers that are laminated and feature coated interiors. Self-locking covers and rolled edges ensure that contents remain inside,Hilton Funeral Supply allowing the container to be lifted and moved. These features eliminate issues common with standard cremation trays.

Profile icon
Whippingdot

To open a file use this:

file = open("filename.txt", "w")

you can change the "file" to any variable name, and you can change the "filename.txt" to your file name.
The "w" means open with write. You can change it to "r" for open with read. There are more but I think that is all you need.

Profile icon
NicJKincheloe

@Whippingdot
Thanks, now my next problem is getting the count. I updated the first file(Hunting2019.csv) so I'm focused on that. the first line of the file reads as shows (Prince Edward County,Sportsmans Hunting and Fishing,25)The way I am trying to do it is if the user inputs that hunting license it prints the number 25, but it comes out as zero. My problem is I dont know how to read that last number

Profile icon
Whippingdot

If you put all the stuff on separate lines then you can do this:

varholdingfilename[2]

you can change varholdingfilename to whatever name you chose your variable that holds the file to be. What this line does is it returns the thing stored on the third line of the file(which, if you separate the stuff into separate lines, is 25). It takes from the third line as indexing in python starts at 0. This means varholdingfilename[0] would take what is stored in the first line of the file.


@NicJKincheloe

Profile icon
NicJKincheloe

@Whippingdot
im not just looking for that certain license that was an example. the csv file is full of different licenses and it would tak way too long to seperate it all into different lines

Profile icon
Whippingdot

Wait, the "Sportsmans hunting and fishing" is the hunting license right?

@NicJKincheloe

Profile icon
Whippingdot

Ok, so I have gotten it. This is the answer I have gotten:

file = open("blah.txt", "r") huntingLicense = input("Please enter your hunting license: ") for line in file: if huntingLicense in line: lineContaining = line break index = len(huntingLicense) + 2 if lineContaining[index] == lineContaining[-1]: print(lineContaining[index]) else: print(lineContaining[index: -1])

you can change "file" to whatever name the variable holding the file should be. huntingLicense just gets the hunting license from the user. Then we iterate thru the file. Every line will be equal to the var line one by one. If the hunting license is in that line, then it will store that line in a variable called lineContaining, and it will break the for loop. Then, we create the index which will be equal to the length of the hunting License + 2. This is because your hunting license will be on a line, which is the length, and then you add two for one, the comma, and two, the space. After that we check if the number you want to output is only one digit, as if so, then our program will break. So if it is, we only print the one digit, which is our index, but if not, then we print the series of digits from the index, till -1, meaning the last digit. For this two work you need to have a space between the comma and your number. Please do this and the program will work.

@NicJKincheloe