Not printing after if statment - python

So I have just started learning to use Python, and I am getting a syntax error.
Following the book I am using , here is a simple if statement, followed by a print statement that happens regardless of the if statement.
name = "Doug"
if name == 'Doug' :
print "Hello, Doug!"
print "How are you today?"
The expected output is:
Hello Doug!
How are you today?
if name != Doug, then the output should be
How are you today?
I've done simple ifs a thousand times in C++ and Java, but with brackets. For some reason, the final print comes back with a syntax error.
I am using Python 2.7.8, not Python 3, and using print or print() gives me the same result.
EDIT:
No amount of Newlines in the interpreter version worked, however running the script in a .py file worked flawlessly. For some reason , my book failed to mention this.

Your code works for me if I put it in a .py file and run the file through the interpreter. E.g. python hello.py. If I run the python interpreter interactively, however, then I can reproduce a syntax error at the second print statement.
I think this is just a quirk of interactive mode. I can make it work in interactive mode, too, by putting an extra newline between the two print statements. For what it's worth, the interactive-mode prompting makes me think that it doesn't recognize the end of the if statement until I type that extra newline after it (otherwise, another statement in the if block might follow).

Related

python subprocess.call not showing certain characters

I'm trying to call tree in python with subprocess.call but instead of returning "│" it only returns "�", this also happens with os.system.
using python 3
windows 10
This is the horror of working with character sets in Windows. The application is printing out one of the line-drawing characters. In the MS-DOS code page, that's probably 0x10. Depending on how your terminal is set up, that might print as a vertical line, or it might print as something else. If the app has the option to print out ASCII art ( + - | ), that will be more universal.
I kinda found out how to fix it, it depends where I run the script from, when I run it from the IDE i get the strange "�" character when I run it from the python console I get nothing but when I run it from cmd I get the right output.

How to run multple lines of python code from shell, instead of from a script

I want to run python code from the shell instead of from a script.
I searched everywhere and found suggestions about using echo or Shift+Enter and using ^ at the end of the line, but ^ and shift+Enter didn't work in python shell, and I need to if there is a way to separate commands in the shell.
For example I need to run this from python shell instead of from a script:
If x > y
print ("x is greater than y.")
but non of the options I tried worked.
You need to use : to issue a line separator.
i.e.
if x > y:
print ("x is greater than y.")
If you are asking how to enter Python code in your shell, then try something like
python -c 'if x > y:
print(("x is greater than y")'
assuming you are using a Bourne-compatible shell (Linux etc). This has the major drawback that your Python code cannot easily contain a single quote (though if you know the quoting rules of the shell it's of course not impossible or even unobvious how to create an expression which evaluates to a single quote).
However, the common way to do this (and perhaps the only way on Windows?) is to type the Python code into a file and then run it with
python filename.py
where filename.py is the name of the file where you saved your Python code.
You can of course run simply
python
to enter the Python interpreter in interactive mode, where you can type in Python expressions and have them evaluated immediately. When you type an expression which ends with a colon, Python changes the prompt from >>> to ... to indicate that it expects one or more indented lines. (Type just a newline immediately at the ... prompt to exit this mode.)

im using a python book and I copied the code but it says, multiple statements found while compiling a single statement don't know what's wrong

name = input("Hi. What's your name? ")
print("name")
print("Hi,", name)
input("\n\nPress the enter key to exit.")
It says multiple statements found while compiling a single statement
I'm using a book and tried many different things. Also, I'm a noob at this as you can tell. If you have any suggestions that'll be great
Your code is fine, assuming you are using python 3, but you need to type (or paste) each line, one at a time. Based on what you are seeing, I suspect you are putting it all in at once, without a new line after each line.
If you are using python 2, you'll need to use raw_input rather than input, like this:
name = raw_input("Hi. What's your name? ")
print("name")
print("Hi,", name)
input("\n\nPress the enter key to exit.")
So couple of things:
print("name") will not print the name you captured from the last variable but a string that says name
print("Hi,", name) prints ('Hi,', 'Dmitry') which is probably not what you want, instead do this: ', '.join(["Hi", name]) There are probably other Python 3 conventions but I work in Python 2 so I don't know them all off the top of my head.
input("\n\nPress the enter key to exit.") not sure of the purpose of this line. Seems like a stray line from a block of code and it's not being assigned to any variable. Furthermore it throws an error SyntaxError. What book are you using if I may ask?
You can check the Python version in the HELP>about IDLE tab of IDLE editor or Shell - you seem to be using python 2 as others have stated
If you are able to enter and run one line of code at a time, followed by the next line then you are using the Shell, not the IDLE editor
You should be able to paste the code you have in question into IDLE editor and run (F5) - you should be prompted to save before it is run in the Shell.

Entering multiple lines of code to be run later at the Python REPL [duplicate]

This question already has answers here:
Python, writing multi line code in IDLE
(7 answers)
Closed 6 years ago.
I just picked up a basic book today on programming. The coding language is Python and I have only been trying this out for a few hours but I'm already stuck, because I can't figure out how to write multiple lines of code. For example, when I write print("one") and then hit enter, it just runs it and prints the word, one. How can I have it print the word, one, and then the word, two, on the line below it? Also, when I hit tab it just moves over 4 spaces, or so. I can't figure out how to have it not run the first command, and just give me '>>>' on the next line. So I guess what I'm asking is: What keystrokes do I need to use to get something like:
>>> print("one")
>>> print("two")
Thanks so much!
(Sorry for such a basic question, but I'm totally confused on this one.)
The Python REPL automatically executes each command as soon as it is completely typed in. This is why it is called a "read-eval-print loop". It accepts one input, evaluates it, and then prints the result.
If you want to execute two complete commands at once, you can put a semicolon between them, like this:
print("one"); print("two")
I said "completely typed in" above, because some commands inherently require multiple lines, so Python must accept several lines of input before the command is "completely typed in". Three types of command work like this: flow-control commands (def, while, if, for, etc., which apply to several indented lines below them), multiline expressions (calculations inside parentheses or brackets), or statements that use a backslash (\) at the end of the line to indicate that it is continued on the next line. So if you type in any of the blocks below, Python will wait until the block is completely finished before evaluating it.
if 1 + 1 == 2:
print "True"
else:
print "False"
print(
1 + 1
)
print \
1 + 1
You could also combine these two strategies and type something like this:
print("one"); \
print("two")
Python will wait for both commands to be typed and then run them both at once. But I've never seen anyone write code that way.
Alternatively, you could type several commands together in a different text editor, and then paste them into the Python REPL, e.g., copy and paste the following into your REPL (but you will get results printed between the commands):
print("one")
print("two")
Alternatively, you can probably get almost exactly the behavior you were originally expecting by using a different interface to Python. The IPython Notebook is a good choice, or you could try the Spyder or PyCharm editors, which let you select a few lines of code and run them.
Or, if you have a longer script that you want to run all at once, the best option is to type it up in a text file (e.g., script.py), and then tell python to run it, e.g., type python script.py from a system command prompt (not the Python interpreter), or press F5 in the IDLE editor.
One thing you may want to try is writing your code in a file, say learning.py, and then running that file on the command line with python learning.py.
The best way to get better support for multi line commands in python with a "console" feel is to use ipython qtconsole, or Jupyter qtconsole as its now called: http://jupyter.org/qtconsole/stable/. When using qtconsole, hitting Ctrl-Enter will delay the command from running even if it's not a complex block. You can keep hitting Ctrl-Enter as many times as you want, and then hit Enter to run them all. Hitting up arrow will then bring up the whole block again to edit, cleanly indented unlike the regular ipython console.
Note: this is not ipython notebook, nor the regular ipython console, but a separate thing from either using the same kernel. The qtconsole has some other nice things like better syntax highlighting and inline plotting compared to the terminal.

Getting "invalid syntax" from previously working code

So I have the following code:
f = open('input.txt', 'r')
text = f.read()
data = text.split()
print data
print '<HTML>\n <HEAD>\n </HEAD>\n <BODY>\n <table border="1">\n'
for x in data:
print ' <tr>' + x + '<tr>'
print'</table>\n </BODY>\n</HTML>'
before I tried to installed iPython, it was working with the default Python shell.
But after I installed distribute and pyreadline and then iPhython, the code won't stop giving me syntax errors, as if not a single variable would work, not sure if there is something about python initialization/declaration that I have missed or if something went wrong with doing stuff on the console, but it certainly is driving me crazy and I need to fix it.
P.S. I use Windows 8
Edit:
Was asked for the errors I get, here are screenshots, since I do not get any very specific text-like errors.
In this second one, I edited the code several times to test different things,
hence why I get error for different variables.
Even the simplest of things would give me an error.
P.S.2. I just tried print 'hey' and it gave me the same error, not recognizing the ' token.
Looks like whatever you installed is using Python 3.x, and your code was written for Python 2.x.
In Python 3.x, print is now a function, not a keyword, so you'll have to change all the lines like...
print data
...to...
print(data)
If you need to retain Python 2.x compatibility, add the line...
from __future__ import print_function
...at the top of the code.

Categories