Hello everyone I was working on a exercise and also new to the syntax of the use of python. I tried to write this code to show the Max Number:
def maxNum(a,c):
if a>c:
return a
else:
return c
print(maxNum(16,20))
a little background when I use maxNum(16,20) or print(maxNum(16,20)) I get a SyntaxError: invalid syntax in the interactive shell however when I use a new window and run the above script the answer 20 is shown. Why is it the above script has to be ran from a new window and not in the shell it to work? In addition is there a website that shows when or how to indent? Thanks
You need to validate the function definition by pressing [ENTER] and then put the rest of code
def maxNum(a,c):
if a>c:
return a
else:
return c
print(maxNum(16,20))
You have extra spaces on the apparently empty line between the end of the function definition and the print call. With that, the interpreter is expecting more content in the function. To end a compound statement in an interactive session, you need two linefeeds in a row.
Related
I am new to Python but am following along a book closely. I can't figure out why this won't work. When I click return to write a new line below the second "if" statement, the code attempts to run and says invalid syntax, highlighting the second "if"
Image of my code
The interactive window of IDLE knows that a block will follow an if statement, and expects that block to be ended by an empty line. According to the image of your code, you only used a backspace, and then IDLE expects an unindented part of the if instruction, said differently an else or elsif.
TL/DR: use an empty line to end an indented bloc.
The interactive window is... interactive! It executes one statement at a time. If you want to prepare a full script and then execute it as a whole, you must create a new file (menu File/New) write the full script and save it to disk at execution time (F5 of menu Run/Run Module).
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
This is a basic example for defining a function, and when I run this code on Python 3.8.5 by VSC, the result is like below.
>>> def greet(name):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
It says that there is an indentation error but I really don't know why. And also, line 2 is not just blank but according to the result, line 2 seems like blank.
for number in range(5):
print("Thank you")
>>> for number in range(5):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
Same error for 'for'. Can anyone help?
Right now, you are running the Python script line-by-line by placing the VS Code cursor on each line of code and then pressing shift+enter. This utilizes the Python Interactive window afaik. It works fine for running individual lines but if you want to run a collection of lines that include indentation e.g. a for loop or function definition, then select all of those lines together and then press shift+enter. You would need to do this for code that's indented, for example:
def greet(name):
print("Hello, " + name + ". Good morning!")
Alternatively, if you have read Getting Started with Python in VS Code and specifically installed the Python extension for VS Code then you will be able to click the Run Python File in Terminal button (it looks like a Play icon) to run your Python script.
Alternatively, you can launch a Terminal Window in VS Code and manually run the Python script using python3 hello.py (or potentially python hello.py, depending on your Python installation).
You should not press Shift+Enter to run the code in the terminal line by line.
Because the REPL will add an Enter to start a new line automatically like this:
You can select all of the codes then send them to the REPL:
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 create a variable to be used in more than one place to print a line of script
I tried using this to print: print(helped), didn't work, it gave the error you see. I tried doing this: print helped(), gave a syntax error
def helped():
print('''Type
Type
Type''')
weirdness = input('''What function would you like to run?
Type "Help" for possible commands ''')
if weirdness.upper() == "HELP":
print (helped)
When I enter "helped" upon being prompted, I get:
What function would you like to run?
Type "Help" for possible commands helped
I think you need a bit of help
instead of getting the print statement
How do i resolve the issue such that this wont happen?
In Python when calling a function, you must use the parentheses. So since your function's name is helped, you should call it like this:
print( helped() )
You should do this regardless of whether your function has parameters.
On a side note, with Python 2.6, you could have also use the print function without parentheses like this:
print helped()
but this is not the case with Python 3.x
You simply need to execute the function. Currently, the helped() function is defined but not run in your code. Essentially, it's never used. Right now you use print (helped) and should be getting a error when instead, you should call the function
Change
print(helped)
to
print(helped())
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