-1

I am trying to do a simple toto history with a dictionary and function however I have this funny syntax error that keeps appearing that states "unexpected EOF while parsing" on the python shell when I try to runs it. I re-looked over and over however I cannot find the error. I used input for input of integers therefore I do not think that the problem might lie with the input or raw_input. Please help me ! Below are my codes and the error on the python shell.

options()
choice = input ("Enter your choice: ")
print

while choice != -1:
    if choice == 1:
        print("Choice 1")
        for key in toto_book:
            print key + "\t" + "Day: " + toto_book[key][0] + '\t' + 'Winning         Numbers: ' + str(toto_book[key][1] + 'Additional Number: ' + toto_book[key][2]
    elif choice == 2:
        print("Choice 2")
        draw = raw_input("Enter draw date(dd/mm/yy): ")
        if draw in toto_book:
            print (draw + "\t" + "Day: " + toto_book[draw][0] + "\t" + "Winning Numbers: " + str(toto_book[draw][1]) + 'Additional Number: ' + toto_book[draw][2])            
        else:
            print draw + ' cannot be found.'

There is a syntax error at the elif choice == 2: line.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Boeing777
  • 1
  • 3

1 Answers1

0

Updated

As pointed out by @cricket_007, this answer is based on the false assumption that Python 3 is being used. Actually, Python 2 is being used and the only serious problem is that the call to str is missing a closing parenthesis.


You are using Python 3 in which print is a function, not a statement (as it is in Python 2).

This is the line causing the problem:

    print key + "\t" + "Day: " + toto_book[key][0] + '\t' + 'Winning         Numbers: ' + str(toto_book[key][1] + 'Additional Number: ' + toto_book[key][2]

Add parentheses to make print a function call, i.e. print(...):

print(key + "\t" + "Day: " + toto_book[key][0] + '\t' + 'Winning         Numbers: ' + str(toto_book[key][1]) + 'Additional Number: ' + toto_book[key][2])

Also, the call to str() was missing the closing parenthesis.

There is a similar problem on line 15.


Other problems:

  • input() returns a string, not an integer so your if choice == statements will never be true. Either convert choice to an integer with choice = int(choice) after the input(), or use a string in the if statements, e.g. if choice == '1'.
  • The while loop is infinte, and unnecessary for the code shown (perhaps it is a work in progress?).
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Oh I see. Thank you very much however I now have a problem. As I used the dictionary toto_book, my code goes: ``if choice == 1: print("Choice 1") for key in toto_book: print (key + "\t" + "Day: " + toto_book[key][0] + '\t' + 'Winning Numbers: ' + str(toto_book[key][1]) + '\t' + 'Additional Number: ' + toto_book[key][2])`` However the results I gotten from the python shell are not what I inputted in order. As in the keys are jumbled up. Can you help ? – Boeing777 Feb 15 '16 at 05:09
  • What indication is there that this is Python3 and not 2? `raw_input` is a Python 2 function – OneCricketeer Feb 15 '16 at 05:12
  • And yes its a work in progress . The while loop is finite as I have a end program if choice == -1 at the end. I did not post it. – Boeing777 Feb 15 '16 at 05:19
  • @cricket_007: thank's for pointing that out - you're right. I only noticed the first `input()` and assumed Python 3 from that. Actually I was wrong, and therefore also wrong about `choice` being converted to an integer, and again `print` being a function. So the problem was only that `str()` was missing a paren, so at least I got something right :) Hard to believe that the OP accepted this answer - I'd delete it otherwise. – mhawke Feb 15 '16 at 05:45
  • @Boeing: re dict keys, perhaps you iterate over the sorted keys, if that is the same as the order of input? Otherwise you could use an [`collections.OrderedDict()`](https://docs.python.org/2/library/collections.html#collections.OrderedDict) which will keep the keys in order of creation. – mhawke Feb 15 '16 at 05:52
  • @Boeing777: I've updated the answer to correct my invalid assumptions. – mhawke Feb 15 '16 at 06:00
  • @mhawke: Hi I am really sorry but I am currently a student learning programming and is rather new.. May I know how should I use the `collections.OrderedDict()` ? – Boeing777 Feb 15 '16 at 07:09
  • @Boeing777: it's pretty much the same as an ordinary dict. There's documentation with examples [here](https://docs.python.org/2/library/collections.html#ordereddict-objects). So, however you are creating `toto_book` now, just use the `OrderedDict` instead. – mhawke Feb 15 '16 at 07:45