Python unexpected EOF while parsing : syntax error

2024/9/21 16:39:39

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: ")
printwhile 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.

Answer

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?).
https://en.xdnf.cn/q/119426.html

Related Q&A

How can I read part of file and write the rest to another file?

I have multiple large csv file. How can I read part of each file and write 10% of the data/rows to another file?

Encryption/Decryption - Python GCSE [duplicate]

This question already has an answer here:Encryption and Decryption within the alphabet - Python GCSE(1 answer)Closed 8 years ago.I am currently trying to write a program, for school, in order to encryp…

Convert decimal to binary (Python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Kivy Digital Clock Issues

Im trying to add a digital clock to my Kivy program, it seems to be having trouble.Here is the .py:import kivykivy.require(1.10.0)from kivy.lang import Builder from kivy.uix.screenmanager import Screen…

Read a file name and create a column with it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Python : T test ind looping over columns of df

My dataframe is composed of accounting variables and a dummy variable that allows me to identify two types of company. I would like to perform a t-test for every column of my dataframe in order to comp…

I want to understand which line of code outputs **none** in the function

The last line of the output is none can someone explain why pls def just_lyrics():print ("i am a bad coder")print (" i keep trying to learn everday")def double_lyrics():just_lyrics(…

How to clear python console (i.e. Ctrl+L command line equivalent)

OS = Linux[boris@E7440-DELL ~]$ uname -a Linux E7440-DELL 3.17.4-200.fc20.x86_64 #1 SMP Fri Nov 21 23:26:41 UTC 2014 x86_64 x86_64 x86_64 GNU/LinuxFrom python console (Spyder 2.2.4, Python 2.7.5 64bits…

Floating point accuracy in python

Any reason why c shouldnt equal 0.321?>>> from math import ceil >>> a = 123.321 >>> b = a % 60 >>> b 3.320999999999998 >>> ceil(b) 4.0 >>> c = cei…

datetime64 comparison in dataframes

I am struggling with datetime64 comparisons in dataframes to update a column. lets say we have a dataframe df with a date columndf.date.values[0] Out[128]: numpy.datetime64(2015-05-17T22:00:00.00000000…