Hello is use eclipse and pydev, I was wondering why my sample code won't work trying to use the while function.
print("Welcome to the annoying program")
response = ""
while response != "Because.":
response = input("why\n")
print("Oh,ok")
the output is the following:
Welcome to the annoying program
why
Because.
Traceback (most recent call last):
File "/Users/calebmatthias/Document/workspace/de.vogella.python.first/simpprogram.py", l ine 9, in <module>
response = input("why\n")
File "/Users/calebmatthias/Desktop/eclipse 2/plugins/org.python.pydev_2.2.3.2011100616/PySrc/pydev_sitecustomize/sitecustomize.py", line 210, in input
return eval(raw_input(prompt))
File "<string>", line 1
Because.
^
SyntaxError: unexpected EOF while parsing
The input function in 2.x evaluates the input as Python code! Use the raw_input function instead.
With input, your text "Because." is being evaluated, and it's a syntax error because the dot isn't followed by anything.
Related
I want to know if it's possible to make a traceback not show the line of code that caused the error.
I don't want:
>>> print("hi"f)
File "<stdin>", line 1
print("hi"f)
^
SyntaxError: invalid syntax
I do want:
>>> print("hi"f)
File "<stdin>", line 1
SyntaxError: invalid syntax
You can just prevent the error altogether by using try and except
try:
print("hello"f)
except:
print("found error");
Now, as you can see, there is an error on the first line, and when ever python interpreter finds that error, instead of calling a traceback, it just runs the code in the except block...
I just wrote helper function ExcText(), you can see how I used it inside test() function. If you call it as ExcText(False) then lines of code are not printed, if ExcText(True) then they are printed.
Try it online!
def ExcText(show_lines):
import sys, traceback
r = traceback.StackSummary.extract(traceback.walk_tb(sys.exc_info()[2]))
if not show_lines:
for i, e in enumerate(r):
r[i]._line = ''
return ''.join(['Traceback (most recent call last):\n'] + r.format() +
traceback.format_exception_only(*sys.exc_info()[:2]))
def test():
try:
def g():
assert False, 'Hello, World!'
def f():
g()
f()
except:
print(ExcText(False))
test()
Output for ExcText(False):
Traceback (most recent call last):
File "C:\t\test.py", line 16, in test
File "C:\t\test.py", line 15, in f
File "C:\t\test.py", line 13, in g
AssertionError: Hello, World!
Output for ExcText(True):
Traceback (most recent call last):
File "C:\t\test.py", line 16, in test
f()
File "C:\t\test.py", line 15, in f
g()
File "C:\t\test.py", line 13, in g
assert False, 'Hello, World!'
AssertionError: Hello, World!
You can also use code above to catch Syntax Errors, just do compile(program_text, '<string>', 'exec'), where program text is a string containing text of script that needs to be checked, example of code is here. You may also read program text from file through:
with open(filename, 'r', encoding = 'utf-8') as f:
prog = f.read()
You should use compile() instead of exec() when you only need syntax check, because script (program text) may contain some malicious code (like trojan) and exec() will execute it after checking syntax, while compile() will not execute, just checks that it is compilable and has no syntax errors.
Also non-printing of code lines can be achieved by hack with patching linecache.getline() function, like below.
But beware that this solution modifies standard module, so it is a hack that should be avoided if possible. First solution above is better.
Try it online!
def test():
import traceback
traceback.linecache.getline = lambda *pargs, **nargs: ''
try:
def g():
assert False, 'Hello, World!'
def f():
g()
f()
except:
traceback.print_exc()
test()
Output:
Traceback (most recent call last):
File "C:\t\test.py", line 10, in test
File "C:\t\test.py", line 9, in f
File "C:\t\test.py", line 7, in g
AssertionError: Hello, World!
In the above Python code the exact output was coming but after output they were showing some error
def main():
i=1
while i < 3:
inputString = input()
inputStringTwo = input()
inputStringThree = input()
outputString=inputString[0] + inputStringTwo[0] + inputStringThree[0]
print(outputString)
i+=1
main()
Here we are giving input as
Very
Important
Person
The Error was
VIP
Traceback (most recent call last):
File "main.py", line 15, in <module>
main()
File "main.py", line 5, in main
inputString = input()
EOFError: EOF when reading a line
Here VIP is the output
For Question and test cases please refer below link
https://drive.google.com/file/d/1cXmIW56uurB83V4FNRuNw9VhJ7RcYU9j/view?usp=sharing
if you are using an online IDE;
EOFError is raised when one of the built-in functions input() or raw_input() hits an end-of-file condition (EOF) without reading any data. This error is sometimes experienced while using online IDEs. (https://www.geeksforgeeks.org/handling-eoferror-exception-in-python/)
try not to use online IDEs its better to download python
(Python 3.x)So I keep getting a syntax error when reading a file. I've had this problem for a while now. I've been working on other parts of the program so far(not shown), but can't solve this syntax error. I am very confused.
I feel guilty posting about a syntax error but I am out of ideas.
Here is the error: Syntax Error: unexpected EOF while parsing: , line 0, pos 0 # line 8
Code:
def main():
filename = 'p4input.txt'
infile = open(filename, "r")
command = 0
while command != 3 and command < 3:
command = eval(infile.readline()) #Problem here
convert = eval(infile.readline())
print(command)
print(convert)
print("done")
main()
The input file (p4input.txt)
Has the following data:
2
534
1
1101
Complete traceback:
Traceback (most recent call last):
File "C:/Users/Ambrin/Desktop/CS 115/TESTER.py", line 16, in <module>
main()
File "C:/Users/Ambrin/Desktop/CS 115/TESTER.py", line 8, in <module>
command = eval(infile.readline())
File "<string>", line 0, in ?
Syntax Error: unexpected EOF while parsing: <string>, line 0, pos 0
This is happening because when you get to the end of the file, readline() returns an empty string, so you're doing eval(''). You need to check for an empty string and break.
As pointed out in a comment above, you probably shouldn't be using eval. If all your inputs are expected to be integers, you can just use int() instead. You'll still need to check for '' though.
I using the raw_input API in python 2.7
I do:
mass_storage_choice = raw_input("Have you enabled USB mass storage on the phone Yes or No?");
I get:
Traceback (most recent call last):
File "C:\tools\ide\juno\eclipse\plugins\org.python.pydev_2.7.3.2013031601\pysrc\pydevd_comm.py", line 765, in doIt
result = pydevd_vars.evaluateExpression(self.thread_id, self.frame_id, self.expression, self.doExec)
File "C:\tools\ide\juno\eclipse\plugins\org.python.pydev_2.7.3.2013031601\pysrc\pydevd_vars.py", line 376, in evaluateExpression
result = eval(compiled, updated_globals, frame.f_locals)
File "<string>", line 1, in <module>
NameError: name 'Y' is not defined
Any ideas why?
In-case you are using an if...then loop in the code make sure you actually put Y in single quotes.Like this 'Y'.Because I am getting a feeling you are processing the Y input which according to me, means yes.Correct me if I am wrong.
Once you get the input from user pass the input to a variable to be defined that should work.
How can I convert this so it could execute?
Traceback (most recent call last):
File "C:\Users\Shady\Desktop\tet.py", line 14, in <module>
exec test
File "<string>", line 1
print "hello world"
^
IndentationError: unexpected indent
source:
test = ''.join(clientIp.split("test6")[1:])
You might need to use lstrip() on the string to get rid of any leading whitespace before passing it to exec.