Invalid syntax, Unexpected EOF - python

What is wrong with my code?
It says there is an invalid syntax and highlighting the colon?
BTW, I'm doing computing GCSE and this is part of the coursework prep.
I want it to take a letter to repeat and then repeat it an inputted number of times.
letter=input("Please enter a letter to be repeated: ")
number=int(input("Please enter the number of times you want it repeated: ")
for a in range(0,number):
print(+letter)

Remember that when you're debugging, compilers and interpreters will report where an error was first detected, not necessarily where the error actually is. You're missing a closing parentheses on this line:
number=int(input("Please enter the number of times you want it repeated: ")
Add another ) to the end of that line. The interpreter is seeing the opening parentheses for the int function call, then is happily looking through the rest of the file to find its match. When it reaches the end of the file without the parentheses being balanced, it gives up and throws the exception.
As Josh points out, +letteris also invalid syntax. I'm not sure what you were trying to achieve with it so I can't recommend a specific fix, but it needs to go.

You are missing your closing parenthesis for the int() function call. Also you need to remove the + from print(+letter)

Related

What is this error message, and how do I resolve it?

I'm taking google's python tutorial and may have fat fingered a few keys, causing an error. I don't recognize the issue here, and the ctrl+click links it allows me to follow take me to line 1 of the file I'm writing in and to python.exe.
It looks like there's an extra character somewhere in a file path? There are no syntax errors in the code itself as the debugger runs through it just fine.
I'm using Visual Studio Code
None of the code I've written (with my knowledge) is causing this error.
This is the error message I'm getting.
SyntaxError: invalid syntax
>>> & C:/Users/mcgilm1/AppData/Local/Programs/Python/Python37-32/python.exe c:/Users/mcgilm1/Documents/google-python-exercises/basic/string2.py
File "<stdin>", line 1
& C:/Users/mcgilm1/AppData/Local/Programs/Python/Python37-32/python.exe c:/Users/mcgilm1/Documents/google-python-exercises/basic/string2.py
^
Including your code could help us, here is a list of things to check that I found on the web: Debugging
Make sure you are not using a Python keyword for a variable name.
Check that you have a colon at the end of the header of every compound statement, including for, while, if, and def statements.
Check that indentation is consistent. You may indent with either spaces or tabs but it’s best not to mix them. Each level should be nested the same amount.
Make sure that any strings in the code have matching quotation marks.
If you have multiline strings with triple quotes (single or double), make sure you have terminated the string properly. An unterminated string may cause an invalid token error at the end of your program, or it may treat the following part of the program as a string until it comes to the next string. In the second case, it might not produce an error message at all!
An unclosed bracket – (, {, or [ – makes Python continue with the next line as part of the current statement. Generally, an error occurs almost immediately in the next line.
Check for the classic = instead of == inside a conditional.

zybooks for python, whotespace issue

enter image description here
so I keep getting these whitespaces in my output. I have googled but nothing relevant comes back. i have attached my code and my output. can someone tell me what i am doing wrong. I dont have any space in my code, so why is it putting it in my ouput? this is a common problem form me in zybooks and i would really love to know what i am doing wrong so i can get this done and move on to the next assignment without spending 2 hours on a simple exercise.
If you're asking why there are spaces between your outputs of favoriteColor, etc., it's because Python's print function automatically adds spaces between each of its arguments. If you don't want this happening, you can change your code in two ways:
Either add a sep argument to your print, telling Python to add an empty separator between your arguments:
print('\nFirst password:', favoriteColor, chr(95), petName, sep='')
Or, you can append your arguments together yourself, as strings:
print('\nFirst password:', favoriteColor + chr(95) + petName)
Also, I should mention that if you need an underscore, you don't need to call chr(95) -- you can just write the string '_'.

Python error in a colon.. cant find an answer

As part of a piece of python work I have to ask for a first name and surname, then repeat it three times if it is correct. When I try it Python puts a syntax error on the colon. What do I need to do to correct it?
Basically whats going on
I've already tried removing the colon, bringing the if line down to one equals, and looking for bracket errors, but I cant find anything.
The colon should be at the end,
if input == yes:
However I cannot see how this can work? Is that the full code? If so, what is the yes variable?
If it was intended to be a string, the line should be,
if input == "yes":
The "" tells Python that it is a string (a word).
A link to Python syntax: https://en.wikipedia.org/wiki/Python_syntax_and_semantics

Syntax error with if statement in python 2.7

I'm having trouble with a "Syntax Error: invalid syntax" message in python code that keeps moving the goals posts on me.
Here's the sample code:
another_answer = int(raw_input("> ")
if another_answer == 1:
print "The pirate's body slowly goes limp and he whispers to you......."
print "Good luck my son. You might need this."
get_gold()
print: "Do you want to continue to the next room?"
When I run this in the shell I get the following:
File "ex36.py", line 179
if another_answer == 1:
^
SyntaxError: invalid syntax
I found this bizarre, and after removing the colon I got an error message on the next line:
File "ex36.py", line 180
print "The pirate's body slowly goes limp and he slowly whispers to you......."
^
SyntaxError: invalid syntax
Similar questions I've found on stackoverflow center around improper spacing and indentation, but I've looked that over and everything seems to be well.
The preceding line is missing a closing parenthesis:
another_answer = int(raw_input("> ")
# ^ ^ ^^
# \ \--//
# \-----------?
Python allows expressions to cross physical lines when enclosed in parentheses, so when you don't close parentheses Python continues to look for the rest of the expression. By the time it reaches the : the expression makes no sense anymore and a SyntaxError exception is raised.
You're missing a closing parenthesis on the first line. The colon is simply the first character which couldn't possibly be part of an actual function argument.
You have two problems, and you have to fix both.
The first is that you're missing a closing parenthesis:
another_answer = int(raw_input("> ")
This means that Python is still trying to compile the arguments to the call to int when it gets to the next line. Until the colon, everything is still legal, because you could have been writing this:
another_answer = int(raw_input("> ")
if another_answer == 1
else '42')
But once you get to the colon, there is no valid expression that could appear in, so that's where you get the SyntaxError.
In general, when you get a SyntaxError on a line where absolutely nothing looks wrong, look to the previous line—usually it's a missing ) (or ] or }).
However, if you fix that, you're going to have another problem. Although you claim this:
Similar questions I've found on stackoverflow center around improper spacing and indentation, but I've looked that over and everything seems to be well.
… everything is definitely not well:
another_answer = int(raw_input("> ")
if another_answer == 1:
That if statement is indented to the right of that another_answer statement. That's illegal, and will raise an IndentationError. You can only change indentation levels in Python to enter or exit a block controlled by a compound statement.
You've also got at least one more error in your code:
print: "Do you want to continue to the next room?"
This will raise a SyntaxError. print isn't a compound statement like if or def, so it doesn't take a colon.
It is easy to find you missed a closing parenthesis at the first line.

Weird program behaviour in Python

When running the following code, which is an easy problem, the Python interpreter works weirdly:
n = input()
for i in range(n):
testcase = raw_input()
#print i
print testcase[2:(int(testcase[0])+1)]+testcase[(int(testcase[0])+2):]
The problem consists in taking n strings and deleting a single character from them.
For example, given the string "4 PYTHON", the program should output "PYTON".
The code runs ok, but if I take out the comment mark, the statement print i makes the interpreter give an unexpected EOF while parsing. Any idea on why this happens?
EDIT: I'm working under Python 2.5, 32 bits in Windows.
Are you sure that the problem is the print i statement? The code works as
expected when I uncomment that statement and run it. However, if I forget to
enter a value for the first input() call, and just enter "4 PYTHON" right off
the bat, then I get:
"SyntaxError: unexpected EOF while parsing"
This happens because input() is not simply storing the text you enter, but also
running eval() on it. And "4 PYTHON" isn't valid python code.
I am yet another who has no trouble with or without the commented print statement. The input function on the first line is no problem as long as I give it something Python can evaluate. So the most likely explanation is that when you are getting that error, you have typed something that isn't a valid Python expression.
Do you always get that error? Can you post a transcript of your interactive session, complete with stack trace?
This worked for me too, give it a try...
n = raw_input()
n = int(n)
for i in range(n):
testcase = raw_input()
print i
print testcase[2:(int(testcase[0])+1)]+testcase[(int(testcase[0])+2):]
Note the n = int(n)
PS: You can continue to use n = input() on the first line; i prefer raw_input.

Categories

Resources