work = open("work.txt") # stores a reference to the file content in a variable # for line in work: # loop over the lines # print(line) # print each line ## The example above adds extra line breaks between lines because there are ## line breaks in the original file! Use .strip() to get rid of white space. name = raw_input("Enter a name to search for: ") for line in work: name = name.title() # make user input into title case (name start with uppercase) if(name in line): line = line.strip() # get rid of extra new lines info = line.split() # makes the line into a list of values separated by spaces total_hours = 0 for i in range(2, len(info)): total_hours = total_hours + float(info[i]) print(info[1] + ": " + str(total_hours) + " hours worked this week.") # Alternately, the line below displays all the hours separated # by spaces by using the join method on a space string # print(info[1] + ": " + " ".join(info[2:]))