Visual studio code and python [duplicate] - python

This question already has answers here:
NameError: name '_name_' is not defined [closed]
(3 answers)
Closed 3 days ago.
I am trying to write a script and I put this down
if _name_== '_main_':
Now visual studio code isn't understanding _name_ but I am following a video so I am pretty confused
It gave me an error code that said it was undefined

it should be two underscores instead of one.
if __name__ == "__main__":
print("hello")
More information about this can be found here

Related

How to create an error message in Python? [duplicate]

This question already has answers here:
Manually raising (throwing) an exception in Python
(11 answers)
Closed 6 years ago.
I want to create an error message in Python, i.e. the program should be interrupted and an (if possible coloured) error message should be printed. For instance:
If a == 0:
Error("a should be nonzero")
In Matlab, you can do this using the error instruction. How can you do this in Python? I have found this page but I am not sure that is what I am looking for.
You can raise it like so:
if a == 0:
raise ValueError("a should be nonzero")
or simply by using assert as:
assert a!=0, "a should be nonzero!"

Python SyntaxError? [duplicate]

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 6 years ago.
I am learning a tutorial for Python language and trying to write a basic "Hello World!" program.
But when I do all steps described in the book I receive an error.
>> print "Hello World!"
SyntaxError: Missing parentheses in call to 'print'
Why I am getting this error?
Is my book wrong?
it seems that you're using Python 3.x.
In python 3.x, the print statement is a function and you need to use it as a function like this
print("Hello World!")
Your book is right, but might be outdated a bit. It seems it describes Python version 2, but you try to run your example on the version 3.
Python 3 has changed some features and this one is the most annoying to switch from P2 to P3.
"print" statement changed to function rather than operator as was in P2.
Calling function you should always use parentheses.
So, if you want to run your program in Python3 you should call it:
print("Hello World!")
And that's it.
If you want to use examples from your book as is - install Python2 and it should work.

Python: use main function [duplicate]

This question already has answers here:
Why doesn't the main() function run when I start a Python script? Where does the script start running (what is its entry point)?
(5 answers)
Closed 6 years ago.
I have code with many functions and main, when I am trying to run the code it's not working and showing like it run successfully. When I am run the debugger it's show me that it run only on the functions' names. so I am pretty sure the problem it's with the main. how can i solve it?
main() is not run implicitly (like in C or Java). In Python you have to explicitly call if you want your code to run.
def main():
some_code()
if __name__ == "__main__":
main() # actually run main
Note that main does not have to be named main - it may be arbitrary named function. Moreover, code to run does not even have to be enclosed in any function. Consider file with content like that:
print "abc"
It'll simply print "abc" on standard output.

Execute File from python [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 7 years ago.
Im trying to make a python program, and i just can't find any information on Google on how to just execute a non-python file from python (with an if statement). For Example:
if abc == "abc":
"Execute ThisFile.sh"
EDIT: this was marked as dupe, but I am not trying to run a single command, I want to execute the entire file.
Use subprocess module.
if abc == 'abc':
subprocess.check_output('bash file.sh', shell=True)

loading python functions returns syntax error [duplicate]

This question already has answers here:
Pasting multiple lines into IDLE
(4 answers)
Closed 9 years ago.
I'm getting an error message quoting that I have a syntax error when trying to load my functions.
I can load them one at a time into the IDLE, but when pasting the full script, an error is returned. I believe its to do with the second function calling the first. To test this, the simple code below also returns this error :(.
def hello():
print('Hello there!')
def boo():
hello()
I'm unsure why this happens because the first function is defined before its called in the second. So it should be loaded in memory already shouldn't it?
Thanks for any help you can give. :)
IDLE interactive interpreter can only handle one task at once, you can't do more.
Save the script to a python file (.py extension), and run it.
From the command line:
$ cd /script_path
$ python script_name.py
Or if you want to run it in IDLE:
Ctrl+N - Paste the code - Save - Press F5
Hope this helps!

Categories

Resources