-2

What I've done:

with open('TheData.txt', 'r') as data_file:

        money = data_file.read()
        wednesday_tickets = ['W1', 'W2', 'W3']
        thursday_tickets = ['T1', 'T2', 'T3']
        friday_tickets = ['F1', 'F2', 'F3']

    countW = money.count (wednesday_tickets)
    countT = money.count (thursday_tickets)
    countF = money.count (friday_tickets)

    wednesday = countW * 5
    thursday = countT * 5
    friday = countF * 10

    money_raised = wednesday + thursday + friday

    print("The total money raised for charity is £ " + money_raised)

Question: When I ran my program, I recieved 'TypeError: Can't convert 'list' object to str implicitly' back. I've tried everything in my knowledge but I don't know what I should do to over come this. My aim with this piece of code is to find out how much money was raised by counting how many T1, T2, T3, W1, W2.. there are in the file and multiply them with their price.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Bobby
  • 5
  • 1
  • 5
  • Possible duplicate of [Converting a list to a string](http://stackoverflow.com/questions/2906092/converting-a-list-to-a-string) – Jacob Krall Mar 09 '16 at 15:46
  • Questions seeking debugging help (**"why isn't this code working?"**) must include the desired behavior, *a specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). Please include sample input, the expected output, and the **full text** of any errors or tracebacks. – MattDMo Mar 09 '16 at 15:46
  • It is also possible that OP is trying to get the length of each day, which would be covered under http://stackoverflow.com/questions/518021/getting-the-length-of-an-array-in-python – goodguy5 Mar 10 '16 at 19:34

1 Answers1

0

The problem is in the 3 lines which begin with

countW = money.count (wednesday_tickets)

The count method is expecting a single string, not a list of strings. You seem to want the total of the counts, with one count for each ticket type. You could replace that line by

countW = sum(money.count(ticket) for ticket in wednesday_tickets)

And similarly for the next two lines. This isn't the most efficient approach since it involves a total of 9 passes over the data, though if this is literally about tickets for something like a high school play I would be surprised if efficiency is that much of an issue.

Also, your last print statement won't work since you can't directly combine a string and a number. Instead, use

print("The total money raised for charity is £", money_raised)

and let the print function convert the number to a string for you.

John Coleman
  • 51,337
  • 7
  • 54
  • 119