LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Running Python code (https://www.linuxquestions.org/questions/programming-9/running-python-code-4175658653/)

Gyroman 08-05-2019 11:43 PM

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

freemedia2018 08-06-2019 01:07 AM

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: ")
to

Code:

a = raw_input("Alphabet: ")
Then it will work. Alternatively, be certain you are running the right version of Python. If you would like to stick with Python 2 after the Python Foundation dumps it next year, PyPy has a good alternative interpreter that will likely suit your needs.

pan64 08-06-2019 01:58 AM

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

Gyroman 08-06-2019 02:51 AM

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

Gyroman 08-06-2019 02:55 AM

Quote:

Originally Posted by pan64 (Post 6021979)
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

What are code tags..?

NevemTeve 08-06-2019 03:04 AM

https://www.linuxquestions.org/quest...gs-4175464257/

pan64 08-06-2019 03:59 AM

Quote:

Originally Posted by Gyroman (Post 6021992)

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

see for example here: https://stackoverflow.com/questions/...ntf-in-python3

astrogeek 08-06-2019 03:59 PM

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.