I have a strange error. Why does it happen?
I write code but Python shows me the error, but I don't see that error. I've attached the screenshot.
Code:
def func(q):
def funct(y):
try:
print(y)
exit()
except:
pass
funct(q)
a=['1','2','3','4','5','6','7']
for x in a:
func(x)
Python:
>>> def func(q):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>> def funct(y):
File "<stdin>", line 1
def funct(y):
^
IndentationError: unexpected indent
>>> try:
File "<stdin>", line 1
try:
^
IndentationError: unexpected indent
>>> print(y)
File "<stdin>", line 1
print(y)
^
IndentationError: unexpected indent
>>> exit()
File "<stdin>", line 1
exit()
^
IndentationError: unexpected indent
>>>
>>> except:
File "<stdin>", line 1
except:
^
IndentationError: unexpected indent
>>> pass
File "<stdin>", line 1
pass
^
IndentationError: unexpected indent
>>>
>>> funct(q)
File "<stdin>", line 1
funct(q)
^
IndentationError: unexpected indent
>>>
>>> a=['1','2','3','4','5','6','7']
>>> for x in a:
... func(x)
...
>>>
Notepad++:
It looks like you're copying and pasting the code into your command prompt, which is why its throwing you an error. When typing code in the "interactive python" command line, if you leave a blank line Python interprets that as you having finished writing code, and executes it.
You're much better off saving your code in a .py file, and then in the command prompt using the command
python folder/to/your/file.py
which will run your code, and allow blank lines. See this question for more information.
You could also change directory by using the cd command in command prompt like so
cd folder/to/your
which will "move" your command prompt into the folder containing your python file. That way if you want to run your code, you only need to use:
python file.py
Related
I am working on a very basic problem on Hackerrank.
Input format:
First line contains integer N.
Second line contains string S.
Output format:
First line should contain N x 2.
Second line should contain the same string S.
sample test case
5
helloworld
my code is as: (on PYTHON 3)
n=int(input())
s=input()
print(2*n)
print(s)
I am getting error:
Execution failed.
EOFError : EOF when reading a line
Stack Trace:
Traceback (most recent call last):
File "/tmp/143981299/user_code.py", line 1, in <module>
N = int(input())
EOFError: EOF when reading a line
I tried this method to take input many times and this is the first time I am having this error. Can anyone please explain why?
use a try/except block to handle the error
while True:
try:
n=int(input())
s=input()
print(2*n)
print(s)
except:
break
Hi guys I'm using python3 and install googlefinace module(https://pypi.python.org/pypi/googlefinance) and the example says it's works
>>> from googlefinance import getQuotes
>>> import json
>>> print json.dumps(getQuotes('AAPL'), indent=2)
but I type this code using my terminal access python3
>>> from googlefinance import getQuotes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/googlefinance/__init__.py", line 55
print "url: ", url
^
SyntaxError: Missing parentheses in call to 'print'
so what's the problem please help me
In python3 print syntax contains parenthesis. There for it is giving you syntax error. Use correct print syntax. print (url)
Help! end='' is causing a syntax error and I'm not sure why.
def print_game_board(game_board_marker):
for i in range(len(game_board_marker)):
for j in range(len(game_board_marker[i])):
print(str(game_board_marker[i][j]).rjust(4), end='')
print()
print()
It seems like you're using Python 2.x, not 3.x.
Check your python version:
$ python -V
Python 2.7:
>>> print(1, end='')
File "<stdin>", line 1
print(1, end='')
^
SyntaxError: invalid syntax
Python 3.3:
>>> print(1, end='')
1>>>
Code :
n,X=input(),0
for t in range(int(n)):
eval(input())
print(X)
Traceback (most recent call last):
File "prog.py", line 3, in <module>
eval(input())
File "<string>", line 1
X++
^
SyntaxError: unexpected EOF while parsing
Using raw_input instead of input() in the only solution I am able to find but in python 3.x input is raw_input(): How do I use raw_input in Python 3
any other method?
FYI; I am trying to solve: http://codeforces.com/problemset/problem/282/A
Remove the eval() call.
input() in Python 2 is the equivalent of eval(input()) in Python 3, and if you need to use raw_input() in Python 2 then in Python 3 you need to remove the eval() call.
You'll have to parse the input yourself; ++ is not a valid Python operator, you cannot use eval() to solve that Codeforces problem.
The simplest way to solve the posted problem is to read the input line by line:
import sys, itertools
count = int(next(sys.stdin))
x = 0
for line in itertools.islice(sys.stdin, count):
x += 1 if '++' in line else -1
print(x)
I have a problem with the following code:
inputf = open('test.dat', 'r')
lines = inputf.readlines()
rico_clus_e = []
for line in lines:
line.split()
print line
if (line[0] != '#'):
rico_clus_e.append(float(line[4]))
inputf.close()
My test.dat file is:
# specn rico_all rico_all_e rico_clus rico_clus_e rico_farclust rico_far_e extin
a119 1.07038692 0.11109547 0.61473431 0.15063627 0.32590239 0.14777812 0.207
And this gives the following output in my terminal:
# specn rico_all rico_all_e rico_clus rico_clus_e rico_farclust rico_far_e extin
a119 1.07038692 0.11109547 0.61473431 0.15063627 0.32590239 0.14777812 0.207
Traceback (most recent call last):
File "test.py", line 8, in <module>
rico_clus_e.append(float(line[4]))
ValueError: could not convert string to float:
I'm quite confused by this. It had nothing to do with spaces, I checked them all. And if you change 4 by 1, 2 or 3 this works, so it must have something to do with the test.dat file, but I can't seem to figure out how. I'm using python 2.7.3.
line.split() on its own does nothing to line. You must store the result of the method call and use that instead.