Same code differs in output across IDE's & pyshell - python

I was running this code on VScode.
age_1 = int(input("age : "))
print(age_1)
It shows this at it's terminal output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'print(age_1)
In Pycharm it shows this output after taking input as 12 :
age : 12
12
In PyShell it works like the Pycharm one. What did I do wrong in the VScode ?
Update :
When ran on a new file on VScode, it ran properly like the Pycharm one. But only for once . Then it shows up same error like before along with some new kinds of error everytime it ran.
Here's the correct one in VScode when ran the 1st time.
PS I:\CSE\LEARNING\PYTHON 3.0\practice> & "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
age : 12
12
Here's one error when ran another time.
& "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
File "<stdin>", line 1
& "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
^
SyntaxError: invalid syntax
Here's another error :
>>>print(age_1) age1 = int(input("age : "))
File "<stdin>", line 1
print(age_1) age1 = int(input("age : "))
^
SyntaxError: invalid syntax
& here's the final one .
>>> print(age1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'age1' is not defined
P.S. I ain't skilled in coding. Explaining with some codes patiently will be appretiated.

The Python ValueError: invalid literal for int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer. ... Then, you can use int() to convert your number to an integer. If this does not work, make sure that the value of a string does not contain any letters.I think you wrote a word that does not change int in the input. Maybe it's a space

Related

strftime returns ValueError

strftime returns Value error. Why?
This is the code
datetime.datetime.now().strftime('%-m')
My output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format string
Based on strftime.org format can depend on system.
On Unix it may needs - like "%-m" but on Windows it may need # like "%#m".
I could test it only on Linux and both works for me: "%-m %#m"
BTW: but if you want to get negative value then you need - before % like "-%m"

How to fix SyntaxError: invalid syntax on a Raspberry Pi?

I have code wrote on Windows, compiles and works correctly, but fails on the raspberry pi
wrote some simple test code to see if it spits out the same error
a = input('this is a test string to split at space \n').split()
print(a)
typed 15:00 (need this data point for the bigger project) and i got this error
15:00
Traceback (most recent call last):
File "test.py", line 1, in <module>
a = input('this is a test string to split at : \n').split(':')
File "<string>", line 1
15:00
^
SyntaxError: invalid syntax
then tried 15 00 at the request of someone on discord, got this message
15 00
Traceback (most recent call last):
File "test.py", line 2, in <module>
a = input('this is a test string to split at : \n').split(':')
File "<string>", line 1
15 00
^
SyntaxError: unexpected EOF while parsing
You may be running different versions of python from windows to Raspberry Pi.
Thanks to this answer:
Note: this is only for Python 2. For Python 3, raw_input() has become
plain input() and the Python 2 input() has been removed.
Reference
a = raw_input('this is a test string to split at space \n').split(':')
print(a)

Invalid literal for int() with base 16

I have a problem with the below code. I am running python script to run exe and pass parameters to it. I get the below error:
ValueError invalid literal for int() with base 16:
However if i put an extra semi colon (which I don't need) after 0 and before double quote it will work. Is something wrong with my string?
program="C:\Program Files (x86)\program.exe"
args='"21022019;A0A1A2A3A4A5A6A7;B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF;1;1982;0"-e fixed -k aa11bb22cc33dd44ee55ff6600112233 -f jf_Creds_python.hex'
subprocess.call([program,args])
This is the full error:
Traceback (most recent call last):
File "<string>", line 249, in <module>
File "<string>", line 53, in main
ValueError: invalid literal for int() with base 16: '"21022019'
I have found a fix but do not really understand it.
program="C:\Program Files (x86)\program.exe"
args='"21022019;A0A1A2A3A4A5A6A7;B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF;1;1982;0"-e fixed -k aa11bb22cc33dd44ee55ff6600112233 -f jf_Creds_python.hex'
subprocess.call([program,{args}])
If I place curly braces around args, it will be successful. It works now, but I'm not fully sure how it is working, if someone might explain it would be great?

Apache Zeppelin: configuration of python interpreter on Windows

I'm having some troubles with running Zeppelin Python notebook on Windows. It looks like some output problem with several SyntaxError: invalid syntax errors. E.g. the code
%python
import numpy as np
print np.random.randn(10)
results in
^
SyntaxError: invalid syntax
[ 0.3144414 0.2340006 0.86525395 -0.24519029 -1.19862891 -0.88127259
-1.64757093 1.39753151 -0.50873422 -1.39649112]
File "<stdin>", line 1
z._displayhook()
^
SyntaxError: invalid syntax
File "<stdin>", line 1
^
SyntaxError: invalid syntax
File "<stdin>", line 1
Similarly when plotting with matplotlib the code is executed, but the actual figures are not shown (perhaps due to the output problems):
%python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
gives an error:
^
SyntaxError: invalid syntax
[<matplotlib.lines.Line2D object at 0x0000000004D737B8>]
File "<stdin>", line 1
z._displayhook()
^
SyntaxError: invalid syntax
File "<stdin>", line 1
^
SyntaxError: invalid syntax
File "<stdin>", line 1
ERROR
I'd appreciate any suggestions or help!
There is an easy work around. In the Interpreters management section
under the python interpreter set the following zeppelin.python property value to python -i
zeppelin.python python -i
(worked for me with v2.7.13)
It appears that Zeppelin doesn't currently support Windows's using python interpreter. You'll need to spin up a different envirornment using a VM.
https://issues.apache.org/jira/browse/ZEPPELIN-1436

Cannot perform operations using rpy2 in Python: "TypeError: argument 1 must be a str, not int"

So I'm trying to get to grips with using the rpy2 module (I am familiar with R but new to Python). Following this tutorial, I first load the library and assign it to the variable 'r' using:
import rpy2
import rpy2.robjects as robjects
r = robjects.r
then I try to perform a simple operation to confirm everything is working:
print(r[2+2])
but I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\rpy2\robjects\__init__.py", line 248, in _
_getitem__
res = _globalenv.get(item)
TypeError: argument 1 must be str, not int
I'm sure it's just something stupid I'm doing wrong, but any advice would be much appreciated. I'm using python3.4.2 (64bit), rpy2-2.5.6 (64bit) on a Windows 7 machine (64bit).
You should use print(r(2+2)) instead of print(r[2+2]).
When you use r[2+2] you are trying to recover an element corresponding to the index 4 (the result of 2+2) of the r iterable. And your r object doesn't seem to respond to this kind of message.
Ok I think I have figured it out. For R to evaluate the function inside the parenthesis, the function must be in quotes e.g.
r("2+2")
This is what was confusing me because this looks like I'm providing a string.
Oddly I don't print the result (4) by using:
print(r("2+2"))
as this prints:
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
print(r("2+2"))
File "C:\Python34\lib\site-packages\rpy2\robjects\robject.py", line 49, in __str__
s = str.join(os.linesep, s)
TypeError: sequence item 0: expected str instance, bytes found
Instead I just print the result using:
answer = r("2+2")
answer[0]
(Because R is vector based, the initial value of the vector is the answer so you have to index it at the first position, otherwise you get:
answer = r("2+2")
answer
<FloatVector - Python:0x0000000005836EC8 / R:0x00000000047A51A0>
[4.000000]
Thanks for you help
Hefin

Categories