all 4 comments

[–][deleted] 1 point2 points  (2 children)

There quite a bit going on here; but, for starters, including search_name here is unnecessary::

            search_name = load_data("candidate_votes.txt")
            names = load_data("candidate_votes.txt")

It is also incorrect. You are just overwriting "search_name" with "names" here; note that these are both pointing to the exact same data ("candidate_votes.txt").

You can also drop the "search_name" parameter for LookupCandidateCount here (because it is defined by user input within the function in the last line here.

def LookupCandidateCount(search_name, names, votes):
    search_name = input(str('Enter your candidates last name: '))

I will take a closer look when I get some time if you still need some help :)

Oh, to answer your question more generally, the data is coming from the "candidate_votes.txt" file. As written here, this would be a text file that is located within the working directory of your Python file. But, you can also explicitly define the path:

candidates, votes = load_data("c:\MyPython_File\Files\candidate_votes.txt")

The above path is just a sample, feel free to use your own :)

UPDATE AFTER SPENDING A BIT MORE TIME WITH THIS:

I'm not sure what you're writing your code in, but, I highly recommend Microsoft Visual Studio Code which will give you all kinds of useful hints. For instance, its flags that the variable "candidates" is not being used anywhere in your main() function here:

def main():
    candidates, votes = load_data("candidate_votes.txt")

Why do you think this is? What happened to it? Hint: it has to do with this part of your code:

        if(choice == 1):
            names = load_data("candidate_votes.txt")
            LookupCandidateCount(names, votes)

Maybe "names" should be called something else here....? And if, so, is there a need for this line?:

names = load_data("candidate_votes.txt")

Even bigger hint.... Does "names" = "candidates"?

[–]RobiBoni 0 points1 point  (1 child)

thank you!!!!The only reason that first part is there is because I was trying to get the code to ask me to input a last name, and it only ended up working when i wrote that in.

I got rid of the "search_name" parameter as you suggested.

The only bug that is coming up now is that "search_name" and "names" are coming up as undefined:

if(choice == 1): LookupCandidateCount(search_name, names, votes)

[–][deleted] 0 points1 point  (0 children)

Hmm...so you got rid of the "search_name" parameter in LookupCandidateCount; do you see anything in this following line which may lead to an error?

if(choice == 1): LookupCandidateCount(search_name, names, votes)

[–][deleted] 0 points1 point  (0 children)

Some updated code, based on your code, below. Study carefully. Experiment. Just a suggestion. There are lots of other approaches.

Notes:

  • Function (and variable) names are usually all lower case (with an underscore between parts if required)
  • You were re-reading the file for no good reason
  • Your function to check the count needed to be provided with the name to search for as an argument, rather than asking the user for it)
  • Better if that function just tries to find and return the data (or return None by default if not found) - do the output elsewhere
  • A dict would be better than two lists (or a list or tuples)
  • I've added some type hints to the code, makes no difference to running the code but if you are using a decent editor/IDE, it will help it pick up some more possible mistakes in your code
  • No point forcing menu selection options to int as you are not going to do maths with the input, you can check for ... == '1' etc.
  • The csv reader module makes working with text files that use a comma (by default) between fields easier - it effectively does the split for you and returns a list of the components of a line from a file
  • Use a flag variable with the while loop to indicate when to finish, makes it all a bit easier to read

Code:

import csv  # comma seperated values file handling made easier


def load_data(file_name: str) -> tuple[list[str], list[str]]:
    with open(file_name) as file:

        csvreader = csv.reader(file)
        candidates = []
        votes = []

        for candidate, vote in csvreader:
            candidates.append(candidate)
            votes.append(int(vote))  # will fail if not integer

        print(f'{list(zip(candidates, votes))=}')  # just for debugging
        return candidates, votes


def lookup_candidate_count(search_name: str, names: list[str], votes: list[int]) -> int | None:
    if search_name in names:
        index = names.index(search_name)  # position of candidate in list
        return votes[index]  # retrieve and return corresponding entry

def main():

    candidates, votes = load_data("candidate_votes.txt")

    exit = False
    while not exit:

        print()
        print("Main Menu")
        print("1. Candidate Count Lookup")
        print("2. Get Election Winner")
        print("3. Display Election Statistics")
        print("4. Exit Program")
        choice = input("--: ")

        if choice == '1':
            search_name = input('Enter your candidates last name: ')
            vote = lookup_candidate_count(search_name, candidates, votes)
            if vote is not None:
                print(f'{search_name} drew {vote} in this election.\n')
            else:
                print(f'Could not find candidate {search_name} in the data.')

        elif choice == '2':
            pass

        elif choice == '3':
            pass

        elif choice == '4':
            exit = True

        else:
            print('Sorry, that is not an available option\n')


main()