I have the following python code:
print 'This is a simple game.'
input('Press enter to continue . . .')
print 'Choose an option:'
...
But when I press Enter button, I get the following error:
Traceback (most recent call last):
File "E:/4.Python/temp.py", line 2, in <module>
input('Press enter to continue . . .')
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
P.S. I am using python IDLE version 2.6 on Windows 7.
Related problem in IPython: Why does the IPython REPL tell me "SyntaxError: unexpected EOF while parsing" as I input the code?
For Python 2, you want raw_input, not input. The former will read a line. The latter will read a line and try to execute it, not advisable if you don't want your code being corrupted by the person entering data.
For example, they could do something like call arbitrary functions, as per the following example:
def sety99():
global y
y = 99
y = 0
input ("Enter something: ")
print y
If you run that code under Python 2 and enter sety99(), the output will 99, despite the fact that at no point does your code (in its normal execution flow) purposefully set y to anything other than zero (it does in the function but that function is never explicitly called by your code). The reason for this is that the input(prompt) call is equivalent to eval(raw_input(prompt)).
See here for the gory details.
Keep in mind that Python 3 fixes this. The input function there behaves as you would expect.
In Python 2, input() strings are evaluated, and if they are empty, an exception is raised. You probably want raw_input() (or move on to Python 3).
In Python 2.x, input() is equivalent to eval(raw_input()). And eval gives a syntax error when you pass it an empty string.
You want to use raw_input() instead.
If you use input on Python 2.x, it is interpreted as a Python expression, which is not what you want. And since in your case, the string is empty, an error is raised.
What you need is raw_input. Use that and it will return a string.
Related
I am running below the addition of two numbers in jupyter
'''
This program `summationoftwonumbers` and displays their results
'''
A = 1
B = 5
print('Sum of Numbers:',A+B)
It is running fine giving an output as "Sum of Numbers: 6"
But when running a file by using PyLint, summationoftwonumber.ipynb gives the below errors.
summationoftwonumbers.ipynb:1:0: C0114: Missing module docstring (missing-module-docstring)
summationoftwonumbers.ipynb:1:0: W0104: Statement seems to have no effect (pointless-statement)
I do not understand why this is happening.
It's that you used quotes to write a comment, which in some cases creates a docstring. Python is warning you that the statement has no effect in your program. To get rid of the warning you could rewrite the line:
''' This program summationoftwonumbers and displays their results '''
as a normal comment:
# This program summationoftwonumbers and displays their results
Whenever I Make A Mistake In The Python Terminal, I Come across the three dots(as given below), However When I Then write the correct code, it gives a syntax error, but when I write the same code again, I get the desired output, Could someone please explain as to why this happens and what exactly happens.
Thanks.
>>> age=14_567_3745_4
>>> print(age0
... print(age)
File "<stdin>", line 2
print(age)
^
SyntaxError: invalid syntax
>>> print(age)
1456737454
As indicated by #MisterMiyagi it is expecting you to just complete the statement and not write it all over again, something like this:
>>> age = 1341
>>> print(age
... )
1341
What you have run into is called 'implicit line joining' in python. Have a look at the docs, https://docs.python.org/2.0/ref/implicit-joining.html
Input function is only accepting integers as an input otherwise I receive this error message when I run using REPL in python 3:
Entry:
b
Traceback (most recent call last):
File "Ex7.5.py", line 1, in <module>
a = input("Entry:\n")
File "<string>", line 1, in <module>
NameError: name 'b' is not defined
Need my code to accept both letters and numbers as input and cant understand why it's not taking b as a string and printing it?
Literally just trying to get this print input function to work currently to then use later in other functions.
If I run the same code with integers only it works no problem.
a = input("Entry:\n")
print(a)
print(type(a))
The answer I'm expecting is b.
This error is occurring because you're running the code in Python 2, not Python 3. input() in Python 2 will evaluate what you give it, in this case as a variable name, which doesn't exist; while input() in Python 3 will keep it as a string. For more details see What's the difference between raw_input() and input() in python3.x?
How to use the correct Python version is another question, but it looks like you're making some progress in the comments so far.
#!/usr/bin/env python
from subprocess import call
try:
call(['echo', "aabbccddee".decode('hex')])
print 'Input without \\x00 works!'
except Exception as e:
print 'Input without \\x00 throws exception: ' + str(e)
try:
call(['echo', "aabbccdd00ee".decode('hex')]) # The input contains \x00
print 'Input with \\x00 works!'
except Exception as e:
print 'Input with \\x00 throws exception: ' + str(e)
Returns the following (tested with Python 2.7 on RHEL 6):
▒▒▒▒▒
Input without \x00 works!
Input with \x00 throws exception: execv() arg 2 must contain only strings
Questions:
Is this expected behavior by subprocess.call or might this be a bug?
Is there any way I can pass binary data (directly) containing \x00 to another executable within a python script? It will probably work using shell=True, but I would prefer another way, if there is any.
Note:
>>> type("00".decode('hex'))
<type 'str'>
Running echo $'\x00' directly works as expected.
EDIT
This seems to be correct behavior after more research. Due to the execve(2) semantics it is not possible to pass a string containing a null byte as argument.
See:
$ echo $'\x55\x55\x55\x00\x55\x55'
UUU
However, I couldn't really find anymore info on this (second question especially, as it seems shell=True won't help either (all data after the null byte wouldn't be passed)). I leave this question open for a while, maybe someone can provide a deeper insight or share some useful resources on this.
This seems to work in python 2.7, but not python 3. Is there an easy way to make a set a list in python 3 that I am missing? Thanks in advance.
mylist = [1,2,3,4,5]
list(set(mylist))
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: 'list' object is not callable
Sorry if this has been asked before, I did a quick search and didn't see an answer specific to python3.
list(set(...)) works fine. The error indicates the 3.x version of the code has a variable called list or set, shadowing the built-in function. Perhaps you renamed mylist to list? Rest assured, that mistake would provoke the exact same error message in Python 2.