1
if Class.lower() == ("classa"):
    print("Showing results for ClassA")
NewDict = {}
with open ("ClassA.txt","r") as A:
     for line in A:

my list is below and I would like to make the user's name their key and their score the value.

3
Dominic
1
Eric
7
Joe
4
Sasha
8
Olivia
6
Callum

I am stuck on making the users name, e.g. Dominic the key for the dictionary and their score as their value. I cannot do this because in my text file they are on different lines the score being on the first line then every other line and the name being on the second line and every other line from there.

timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Where are you stuck? – timgeb Mar 10 '16 at 13:59
  • Also what does your file actually look like? Does it have bulletpoints and blank lines? Is the number of lines guaranteed to be even? – timgeb Mar 10 '16 at 14:05
  • I am stuck on making the users name e.g. Dominic the key for the dictionary and their score as their value. I cannot do this because in my text file they are on different lines the score being on the first line then every other line and the name being on the second line and every other line from there. there are no bullet points on the file its just a textfile – ActivateCodeMode Mar 12 '16 at 13:01
  • can you give me any feedback on my answer? Did you get it to work? Thanks. – timgeb Mar 14 '16 at 08:00
  • Most of your assumptions are correct however names are used again three time each, the code which you have given would work if the name was not repeated. – ActivateCodeMode Mar 14 '16 at 09:02
  • You should have mentioned that. I updated my answer. Is it ok now? – timgeb Mar 14 '16 at 09:11
  • The answer i get is defaultdict(, {'Mohammad': [10]}) defaultdict(, {'Mohammad': [10, 8]}) defaultdict(, {'Mohammad': [10, 8, 9]}) and this continues for each person creating a massive print and also at the bottom it gives this error:Traceback (most recent call last): File "H:\Computer Science\Python Controlled Assessment\New folder\task 3.py", line 26, in scores[name.strip()].append(int(score)) – ActivateCodeMode Mar 14 '16 at 09:46
  • Then you are either not doing exactly what I am doing or your inputfile is malformed. – timgeb Mar 14 '16 at 09:48
  • the textfile format is exactly the same, copy and pasted your format and returned the same error – ActivateCodeMode Mar 14 '16 at 09:49
  • Did you also copy and paste my solution, into a fresh file? The error is 100% on your end, the solution works as I posted it. – timgeb Mar 14 '16 at 09:50
  • Also please post the type (!) of error you are getting. – timgeb Mar 14 '16 at 09:53
  • in a new text file it returns an error saying name is not defined, will continue this in a few hours :/Traceback (most recent call last): File "H:/Computer Science/Python Controlled Assessment/New folder/dhu9qeht[ghq[r.py", line 7, in scores[name.strip()].append(int(score)) NameError: name 'name' is not defined – ActivateCodeMode Mar 14 '16 at 09:54
  • `NameError: name 'name' is not defined` This 100% means you did not copy and paste the code as I provided it. The line `for score, name in zip(f, f):` is missing where `name` gets defined, or your indentation is wrong. – timgeb Mar 14 '16 at 09:56
  • After doing it again it seems to have worked, will make sure it works on this Wednesday and if so will give you best answer<3 thank you for your help also any chance you know how I can only print the highest score the user has achieved? – ActivateCodeMode Mar 14 '16 at 16:00
  • You could simply call the `max` function with a value-list to get the maximum score. If your question goes beyond that, feel free to open a new one. – timgeb Mar 14 '16 at 18:06

1 Answers1

1

The following solution makes a few assumptions. The first is that the number of lines in your file is even because there is always a line with the score followed by a line with the name. It also assumes that there are no duplicate names and the format you are showing is honored.

With that said, this solution shows you how to build a dictionary mapping names to scores and should point you in the right direction.

>>> with open('ClassA.txt') as f:
...     scores = {name.strip():int(score) for score, name in zip(f, f)}
... 
>>> scores
{'Callum': 6, 'Sasha': 4, 'Olivia': 8, 'Joe': 7, 'Dominic': 3, 'Eric': 1}

Further reading: How do I read two lines from a file at a time using python

For people stumbling across this question using Python 2: consider using itertools.izip for memory efficiency, especially if the file is large.

edit:

Most of your assumptions are correct however names are used again three times each, the code which you have given would work if the name was not repeated.

You should have mentioned that. In that case, I suggest that you append the scores to a list. You can't have the same key twice in a dictionary.

>>> from collections import defaultdict
>>> scores = defaultdict(list)
>>> 
>>> with open('ClassA.txt') as f:
...     for score, name in zip(f, f):
...         scores[name.strip()].append(int(score))
... 
>>> scores
defaultdict(<type 'list'>, {'Callum': [6, 12, 18], 'Sasha': [4, 10, 16], 'Olivia': [5, 11, 17], 'Joe': [3, 9, 15], 'Dominic': [1, 7, 13], 'Eric': [2, 8, 14]})

The inputfile I used for the demo looks like this:

1
Dominic
2
Eric
3
Joe
4
Sasha
5
Olivia
6
Callum
7
Dominic
8
Eric
9
Joe
10
Sasha
11
Olivia
12
Callum
13
Dominic
14
Eric
15
Joe
16
Sasha
17
Olivia
18
Callum
Community
  • 1
  • 1
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Thank you for you help, the problem which i was facing was that in my text file i had started a new line which was empty which prevented the code from working as an empty line was not recognised as an integer. – ActivateCodeMode Mar 16 '16 at 12:25