LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2019, 11:43 PM   #1
Gyroman
Member
 
Registered: Nov 2014
Location: Australia
Distribution: Mint 19.1 Sony E series VPCEB36FG
Posts: 61
Blog Entries: 4

Rep: Reputation: 1
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
 
Old 08-06-2019, 01:07 AM   #2
freemedia2018
Member
 
Registered: Mar 2019
Distribution: various automated remasters
Posts: 145
Blog Entries: 2

Rep: Reputation: 77
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.
 
2 members found this post helpful.
Old 08-06-2019, 01:58 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,943

Rep: Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080
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
 
4 members found this post helpful.
Old 08-06-2019, 02:51 AM   #4
Gyroman
Member
 
Registered: Nov 2014
Location: Australia
Distribution: Mint 19.1 Sony E series VPCEB36FG
Posts: 61

Original Poster
Blog Entries: 4

Rep: Reputation: 1
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
 
Old 08-06-2019, 02:55 AM   #5
Gyroman
Member
 
Registered: Nov 2014
Location: Australia
Distribution: Mint 19.1 Sony E series VPCEB36FG
Posts: 61

Original Poster
Blog Entries: 4

Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
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..?
 
Old 08-06-2019, 03:04 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,803

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
https://www.linuxquestions.org/quest...gs-4175464257/
 
2 members found this post helpful.
Old 08-06-2019, 03:59 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,943

Rep: Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080Reputation: 4080
Quote:
Originally Posted by Gyroman View Post

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
 
2 members found this post helpful.
Old 08-06-2019, 03:59 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,160
Blog Entries: 11

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
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!
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] NEED HELP! Want to code PERL and Python on VS Code on Fedora KDE 27 GHOSTIN7HESHELL Linux - Software 13 02-15-2018 05:00 PM
[SOLVED] Learning Python, how should I implement the following problem in Python 3 code? rblampain Programming 8 05-29-2016 10:53 AM
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration