I am trying to write a program to find the sum of a list using recursion in python and my code is this
value = 0
def sum_list(alist):
global value
if len(alist) == 0:
return value
value += alist.pop()
return sum_list(alist)
print sum_list(range(10))
But when i am executing this script I am getting a weired error.
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no "print" mailcap rules found for type "text/x-python"
I searched for it but couldn't get why this error is coming.
Help will be appreciated
Your code is valid Python 2. (It's not valid Python 3 because the print statement would have to be different.)
You don't say how you are actually running this code, but it looks like it is not actually being interpreted as a Python program. The error message is coming from /usr/bin/print, so I think you have managed to get this interpreted as a shell script somehow, and the "print" on your final line is running /usr/bin/print. That's obviously not what you want.
If you are executing this inside a source file, try putting the following line at the top of it, to tell the shell to run this as a Python program:
#!/usr/bin/env python
Alternatively, run it using
python myfile.py
I had this problem, it was an oversight at the terminal prompt. I was typing print instead of python
$ print sum.py
$ Error: no "print" mailcap rules found for type "text/x-python"
Correct way:
$ python sum.py
$ program runs successfully...
The error message comes from Perl. No idea how you managed to call it with this Python code.
see also http://www.perlmonks.org/?node_id=113525
Related
I am a newbie trying to use Python (2.17.15 via Anaconda) on Visual Stodio Code on my Mac. I have the following simple code:
def function(x):
y = x + 2
return y
This code is giving me the usual trouble, an indentation error:
return y
^
IndentationError: unexpected indent
>>> return y
File "<stdin>", line 1
return y
^
IndentationError: unexpected indent
>>>
Needless to say that Jupyter or Spyder have no problem with this. I checked that on VSC tab gives 4 spaces. All similar questions are related to this, but I cannot fix it.
Other, built in functions of Python work fine.
Please give me some help or tips since I do not know how to escape this.
UPDATE
Installing again Python3 this simple code DOES work on Sublime but still not on VS Code. I still get the same error in VS Code.
UPDATE2
So, another update. If I change from return to print and instead of shift-command debug and run the code then it works.
Any idea what is going on?
This looks like it's because you're running the code with Shift+ENTER.
VS Code has the following 2 bindings for Shift_ENTER:
I believe that you're seeing the 2nd of these, which says "Run Selection/Line in Python Terminal. I suspect you have the focus on the return y line, and so it's only running that single line of code.
If, instead of Shift+ENTER, you use the Run Code command in VS Code, you should see it work fine:
You might well think "OK...so if I select all of the code this will work, right?" and I agree...this feels like it should work. However, I see a similar issue. I'll see if I can work out why, but for the moment you can use the Run Code command in VS Code and that will do what you want. If you highlight the code you want to run, that will limit what gets executed.
Run Code can be executed with Ctrl+Alt+N
It looks like this issue (that selected code doesn't run correctly with Shift+ENTER) is a bug that's being tracked by here: https://github.com/Microsoft/vscode-python/issues/2837
And a work around (not ideal) is to add code before/after your function that is NOT indented, and then select and execute those lines too:
print("this...")
def function(x):
y = x + 2
return y
print("...now works if you select all these lines and Shift+ENTER!")
This is a bug from the python extension, which you need to run chunks of code in the interactive mode.
So in the example code below:
for lastRun in list(d_RunPanelsPresent.keys()):
# some indented commands
logFile = f"/nexusb/Novaseq/{lastRun}/logPPscript.txt"
if not os.path.isfile(logFile):
with open(logFile, 'w+') as f:
pass
else:
pass
If I highlight as follows (note where the cursor is):
I will get an error.
The solution is to highlight the code from the very left of the code editor, as shown below:
This works for me 100% of the time.
Forgetting a semicolon at the end of the function definition produces the same error.
While learning Python and browsing the internet I stumble upon a piece of code in w3schools.com. I tried to run it using their built-in site Python IDLE and using my own Python 3.9.0 Shell. What I got is two different outputs.
I want to know which output is the correct output and why is it providing two different outputs.
The Codes and Its Output
Built in site Python IDLE
Python 3.9.0 Shell
Notice that the number 21 is only printed(outputted) once when running the code using Built-in site Python IDLE, while it is printed(outputted) twice when running the code using Python 3.9.0 Shell.
My Own Debugging
I have tried a simple print statement debugging. Checking the result there is only one different, using Python 3.9.0 Shell the last return line is executed and it outputted the last result while using the Built-in site Python IDLE the last return is either not executed or not outputted, in this case, I believe it is the former, and I believe the correct output is the Python 3.9.0 Shell, but I have no idea why are there two different output.
Print Statement Using Python 3.9.0 Shell Result Part 1 Result Part 2
Print Statement Using Built-in site Python IDLE Result Part 1 Result Part 2
Source Code
def tri_recursion(k):
if(k>0):
result = k + tri_recursion(k-1)
print(result)
else:
result = 0
return result
tri_recursion(6)
You have added a return result statement at the end. For any IDE, unless you print that value, it wouldn't be displayed. However, IDLE prints the return value as well. Technically, both outputs are correct, since both interpreters are configured to do different actions. As a small example,
def foo():
return(1)
Running foo() on IDLE gives >>> 1, whereas it gives nothing on other IDE's, as there is no print statement
Both are correct.
The built-in python on the site is showing you the output of the execution of your program. This is equivalent to running the following program:
if __name__ == '__main__':
tri_recursion(6)
add this at the end of your code, save it as test.py and run it like this:
python test.py
the results will be the same
The python shell is showing you the output of the REPL (Read-Eval-Print-Loop), the print statement will print to the screen, but the return value of the function is also printed, because of the REPL, there's no way to avoid this, it is so by design.
You could design your function to not return anything, but it wouldn't be recursive anymore.
both are correct you need to understand that the python shell is printing statement's output.
when you write :
>>> tri_recursion(6)
it will execute first all the print inside function then it prints value that the last call returns.
I am trying to use php exec() function to execute a python file. When i try something like this:
echo exec("python app.py");
It works but if i omit the python string and keep it like this:
echo exec("app.py"),
it gives this error: Syntax error: word unexpected (expecting ")")" which also shows up even if i change from python to another language other than php like JavaScript
app.py only contains one code line: print("Hello World").
What am i doing wrong please as this is my first time using the exec() function.
app.py should include a Shebang so it will look like this:
#!/usr/bin/python
print("Hello World")
echo exec("./app.py")
Hope this works and helps!
~ PanTrakX
I am new to PyCharm and I have 'Process finished with exit code 0' instead of getting (683, 11) as a result (please see attachment), could you guys help me out please? Much appreciate it!
That is good news! It means that there is no error with your code. You have run it right through and there is nothing wrong with it. Pycharm returns 0 when it has found no errors (plus any output you give it) and returns 1 as well as an error message when it encounters errors.
Editors and scripts do not behave like the interactive terminal, when you run a function it does not automatically show the the result. You need to actually tell it to do it yourself.
Generally you just print the results.
If you use print(data.shape) it should return what you expect with the success message Process finished with exit code 0.
exit code 0 means you code run with no error.
Let's give a error code for example(clearly in the below image): in below code, the variable lst is an empty list,
but we get the 5 member in it(which not exists), so the program throws IndexError, and exit 1 which means there is error with the code.
You can also define exit code for analysis, for example:
ERROR_USERNAME, ERROR_PASSWORD, RIGHT_CODE = 683, 11, 0
right_name, right_password = 'xy', 'xy'
name, password = 'xy', 'wrong_password'
if name != right_name:
exit(ERROR_USERNAME)
if password != right_password:
exit(ERROR_PASSWORD)
exit(RIGHT_CODE)
I would recommend you to read up onexit codes.
exit 0 means no error.
exit 1 means there is some error in your code.
This is not pyCharm or python specific. This is a very common practice in most of the programming languages. Where exit 0 means the successful execution of the program and a non zero exit code indicates an error.
Almost all the program(C++/python/java..) return 0 if it runs successful.That isn't specific to pycharm or python.
In program there is no need to invoke exit function explicitly when it runs success it invoke exit(0) by default, invoke exit(not_zero_num) when runs failed.
You can also invoke exit function with different code(num) for analysis.
You can also see https://en.wikipedia.org/wiki/Exit_(system_call) for more details.
What worked for me when this happened was to go to
Run --> Edit Configurations --> Execution --> check the box Run with
Python Console (which was unchecked).
This means that the compilation was successful (no errors). PyCharm and command prompt (Windows OS), terminal (Ubuntu) don't work the same way. PyCharm is an editor and if you want to print something, you explicitly have to write the print statement:
print(whatever_you_want_to_print)
In your case,
print(data.shape)
I think there's no problem in your code and you could find your print results (and other outputs) in the tab 5: Debug rather than 4: Run.
I just ran into this, but couldn't even run a simple print('hello world') function.
Turns out Comodo's Firewall was stopping the script from printing. This is a pretty easy fix by deleting Python out of the Settings > Advanced > Script Analysis portion of Comodo.
Good Luck
I had same problem with yours. And I finally solve it
I see you are trying to run code "Kaggle - BreastCancer.py"
but your pycharm try to run "Breast.py" instead of your code.
(I think Breast.py only contains functions so pycharm can run without showing any result)
Check on tab [Run] which code you are trying to run.
Your starting the program's run from a different file than you have open there. In Run (alt+shift+F10), set the python file you would like to run or debug.
I've tried to run multiple python functions on mac terminal and they all return syntax error, for this program
def spam():
print "R"
spam()
it returned the error:
./test.py: line 1: syntax error near unexpected token `('
./test.py: line 1: `def spam():'
this is really the simplest function I could find.
Just to be clear terminal is running the rest of the program but it can't handle functions.
#!/usr/bin/python
import math
number = int(raw_input("What's your surd?"))
print type(number)
#Just to let us know what the input is
if type(number) == int:
print "Number is an integer"
else:
print "Please enter a number"
value = math.sqrt(number)
#Takes the number and square roots it
new_value = int(value)
#Turns square root of number into an integer
if type(new_value) == int:
print "Surd can be simplified"
print new_value
else:
print "Surd cannot be simplified"
print value
This program runs fine even if it is a bit buggy at the moment but the following program returns the same error as the previous function.
# define a function
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = int(input("What's your number? "))
print_factors(num)
Why terminal is returning a syntax error where there isn't one?
The problem here (at least for that first example) is that you're not using a python interpreter. The terminal is using a bash interpreter for your python code and getting very confused. Use a command like this to execute your code python spam.py . Or first enter the python command interpreter by running python, then entering your code in the command line interpreter.
What might be even easier while getting started is to get an IDE like PyCharm (https://www.jetbrains.com/pycharm/) and running through a couple of their tutorials to get the feel of it.
Your issue is that your shell doesn't know you are running a Python script. You need to make it clear that you should be using the Python interpreter. You can either do this by:
1) Call python test.py at your terminal.
2) Add #!/usr/bin/python at the top of your Python script (you may need to alter the path to the Python executable on your system). Make the script executable, and call ./test.py at your terminal.
The benefits of 2) are that you know what version of Python you will be running your script with (Python 2.x in your case?).
Method 1) will use whatever Python version is encountered first in your PATH, which may be Python 3 or Python 2, depending on whether you have installed Python 3 at some point. The code you have written will work with Python 2.7, but not Python 3.x. Of course you can always explicitly call python2.7 ./test.py.