Why aren't I able to perform simple arithmetic operations in python using Vscode but I can elsewhere [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
essentially when I run something as simple as '3 * 3' in any other IDE I have no issues, however, all arithmetic operations in my Vscode seem to return nothing, the terminal just repeats my PWD essentially.
Note that I am new and learning python but everything else thus far has worked fine, print statements print to the terminal etc but maths docent seem to work.
enter code here: def example(val1, val2):
return val1 * val2
example(2, 6)
Edit: by terminal I mean I am writing the script and clicking the run button in vscode which outputs to said terminal, I have uploaded a SC of what I mean :screenshot of the IDE and terminal output

In some environments, such as the Python REPL (the P stands for "print") and Jupyter Notebook), results are printed out automatically. However, when you run a script from the command line or VS Code, printing isn't automatic. To output something in this case, you need to add a print() command:
def example(val1, val2):
return val1 * val2
result = example(2, 6)
print(result)

Your terminal is running a shell, probably bash not python.
Then, if you want to evaluate that in bash do
$(( 3 * 3 ))

Related

Does Python execute code from the top or bottom of the script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
The community reviewed whether to reopen this question 11 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am working on learning Python and was wondering in what way the scripts are executed what i mean like this if Python runs the code from the beginning of the script or the bottom
Yes, the things at the top will be read first so for example if you create a function at the top and execute it at the bottom your script will work fine.
But if you execute the function first and then define it at the bottom when python gets to execute the function it will not know what you are trying to execute.
This will work
def myFunction():
print "Hello World!"
myFunction()
This will not work (because python doesn't know what myFunction() is because it hasn't seen it before:
myFunction()
def myFunction():
print "Hello World!"
Code executes on the main scope from top to bottom. If you have several functions in the bottom that are being defined but not called and then code in the main scope below them, it would run the code. However, everything that is in the same scope is ran from top to bottom.
def function_1():
returns 3
x = 6
y = 1
# x = 6 is executed first and the y, function is not called here.
if you do the below, function_1/p assignment gets executed first, then x then y assignments
def function_1():
returns 3
p = function_1()
x = 6
y = 1
This can be expanded into several answers but you will understand more how the script executes through examples. I would recommend to try things locally and learn by doing. Good luck!

Python - IndexError: list index out of range while running python code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Please I need assistance with this code. I keep getting the error message above on line 5 and 35 of the code shown below. What is wrong?
output = sys.argv[1]
main()
sys.argv returns:
The list of command line arguments passed to a Python script. argv[0]
is the script name
So, if you execute the script like this python script.py then sys.argv[0] returns script.py which is the name of script.
As there is no other argument, hence calling sys.argv[1] will raises as IndexError
Now consider another scenario when you execute the script as python script.py tom:
In this case calling sys.argv[1] returns the first command line argument which is tom.
Hope, this helps clear your understanding.
you need to pass argument by running file name like below
python file_name arg1
then only it will work

Test Cases in Python Using Doctest [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How do I write a program to divide 2 numbers along with exception handling and test cases in Python 3.6 ? I read that it can be done by importing 'doctest'.
Doctest is a module included in the Python programming language's standard library that allows the easy generation of tests based on the output from the standard Python interpreter shell, cut and pasted into doc-strings.
You can write your code as:
divide.py
def divide(a, b):
"""
>>> divide(8, 4)
2
>>> divide(-12, 3)
-4
>>> divide(4, 0)
"Denominator can't be zero"
"""
try:
result = a//b
except ZeroDivisionError:
result = "Denominator can't be zero"
return result
Each '>>>' line is run as if in a python shell, and counts as a test.
The next line, if not '>>>' is the expected output of the previous line.
If anything doesn't match exactly (including trailing spaces), the test fails.
Running the doctest
You run doctest like this(write the commands in the terminal):
$ python3 -m doctest -v doctest_trial.py , or simply
$ python3 -m doctest doctest_trial.py

Script running on Linux gives NameError [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I have a python script running on a Linux server. The code:
#!/bin/python
databaseRun = input("Do you want to run all databases: ")
if databaseRun == "yes":
print("yes")
else:
print("no")
This returns and error:
Traceback (most recent call last):
File "./db_upg.py", line 3, in <module> databaseRun = input("Do you want to run all databases: ")
File "string", line 1, in <module> NameError: name 'yes' is not defined
Now if I type this code into PyCharm it runs with no problem. What am I missing? Basically you will put yes or no will be the only to answers
You're probably running this with Python 2 instead of Python 3. Try running it with python3 <scriptname> instead of python <scriptname>. And change the shebang line from #!/bin/python to #!/usr/bin/python3 respectively.
In Python 2, input() reads the input as a code instead of a string. Thus when you type yes, it reads it as a variable yes which is undefined. If you want to run this with Python 2 instead of Python 3 as your tag suggests, use raw_input() instead of input(). Python 2's raw_input() returns a string, similar to Python 3's input().

How to use Mathematica functions in Python programs? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'd like to know how can I call Mathematica functions from Python.
I appreciate a example, for example, using the Mathematica function Prime.
I had search about MathLink but how to use it in Python is a little obscure to me.
I tried to use a Mathematica-Python library called pyml but I hadn't no sucess, maybe because this lib looks very old (in tutorial says Mathematica 2 or 3).
Tried compile a source in Wolfram/Mathematica/8.0/SystemFiles/Links/Python but ended with several errors when using python 2.6 (documentation says should work to python 2.3 only).
Pythonika is interesting, but, looks like is just to use in Mathematica notebooks and I would like write .py files who calls Mathematica functions.
So, someone knows a good way to write python programs who uses Mathematica functions and can give me an example?
You can call Mathematica function in Python using the Python MathLink module (the source you found in .../SystemFiles/Links/Python), though you'll need to edit a couple of setup files to get it up and running (support#wolfram.com should be able to help you out there).
To use Prime from Python you would run something like:
kernel.ready()
0
kernel.putfunction("Prime",1)
kernel.putinteger(10)
kernel.flush()
kernel.ready()
1
kernel.nextpacket()
3
packetdescriptiondictionary[3]
'ReturnPacket'
kernel.getinteger()
29
I had found a solution.
Steps:
1-Create a script named runMath with the content:
#!/usr/local/bin/MathematicaScript -script
value=ToExpression[$ScriptCommandLine[[2]]];
(*The next lime prints the script name.*)
(*Print[$ScriptCommandLine[[1]]];*)
Print[value];
2-I gave execution privilege to the file.
sudo chmod +x runMath
3-Moved the file to the execution path
sudo mv runMath /usr/bin/
4-Created a new script called run with the content:
#!/usr/bin/python
from subprocess import *
from sys import *
command='/usr/bin/runMath'
parameter=argv[1]
call([command,parameter])
5-Moved to the execution path
sudo mv run /usr/bin
6-Finally, tested it:
$run Prime[100]
541
$run 'Sum[2x-1,{x,1,k}]'
k^2
$run Integrate[Log[x],x]
-x + x*Log[x]
$run 'Zeta[2]'
Pi^2/6
You can use with or without '. The ' are needed for commands with spaces.
$run 'f[n_] := f[n] = f[n - 1] + f[n - 2]; f[1] = f[2] = 1; Table[f[n],{n,5}]'
{1, 1, 2, 3, 5}
Happy!

Categories

Resources