Running Python code
New to Python.
When running a Python program from the terminal as in; python prog.py a = 'answer' print(a) {produces} ('answer') however when I do python3 >>> import prog.py a = input('Input a word ') {it produces} Input a word answer {then} print(a) {produces} answer So Q1 why the quotes and brackets in the first..? Then for data input: a = input("Alphabet: ") {I get:} mike@mikes-Sony-Laptop:~/Python$ python evolve.py Alphabet: atcg Traceback (most recent call last): File "evolve.py", line 144, in <module> a = input('Alphabet: ') File "<string>", line 1, in <module> NameError: name 'atcg' is not defined mike@mikes-Sony-Laptop:~/Python$ Qn 2 Why does it assume the input string "atcg" is a variable..? Thanks to all |
The problem with atcg happens due to the fact that input() doesn't work in the same in Python 2. If you are going to use Python 2, which you did in the bottom example, you need to change:
Code:
a = input("Alphabet: ") Code:
a = raw_input("Alphabet: ") |
would be nice to use code tags, otherwise the python code is just unreadable.
From the other hand as it was already mentioned python2 and python3 are different. 1. input and/or raw_input 2. the print functionality is completely redesigned and is actually incompatible (between python2 and python3). 3. and obviously there are other differences too |
Thanks
I did not know I was running Python 2 when giving the command: python prog.py so it is python3 prog.py works fine thanks and no I have no interest in continuing with Python 2 Just one further question please.. Formatting output with the print() command looks really confusing.. in the docs.. How do I format number output to just limit the decimal places? like x = 1/3 print("Here is the answer ",x, "to 2 decimal places") regards |
Quote:
|
|
Quote:
|
To add to already posted link, to preserve formatting please place your code snippets inside [CODE]...[/CODE] tags. You may type those yourself or click the "#" button in the edit controls.
This is especially important when posting python code as it cannot be read without proper indentation. Thanks! |
All times are GMT -5. The time now is 02:23 PM. |