Delzun
Posts: 3
Joined: Sat Feb 15, 2014 2:24 pm

(Problem solved) Printout results differ from examples

Mon Feb 17, 2014 7:16 pm

I been trying examples from this site: http://docs.python.org/3/tutorial/controlflow.html but results I get are often different from examples given.

This is what site shows:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else:
... # loop fell through without finding a factor
... print(n, 'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
I used the same code, but got different looking output:
(2, 'is a prime number')
(3, 'is a prime number')
(4, 'equals', 2, '*', 2)
(5, 'is a prime number')
(6, 'equals', 2, '*', 3)
(7, 'is a prime number')
(8, 'equals', 2, '*', 4)
(9, 'equals', 3, '*', 3)
All printed text has annoying '-marks around them. This seems happen always when there is text and variables printed out together.

Some other thing shown in examples have also given to me different result or refused to work at all, giving some errors instead.
What could be causing this and how could I make outputs tidier?
This was done with latest version of Raspbian.
Last edited by Delzun on Tue Feb 18, 2014 5:36 pm, edited 1 time in total.

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Printout results differ from examples

Mon Feb 17, 2014 8:06 pm

The examples from that link are using the latest version of Python, Python 3. Raspian by default uses Python 2.7, and there are some differences. I'm not sure how to 'upgrade' Raspian to Python3, (I'm a Luddite), but it may be as simple as specifying

Code: Select all

python3 example.py
There's probably an environment variable or other setting to default things to 3.

Delzun
Posts: 3
Joined: Sat Feb 15, 2014 2:24 pm

Re: Printout results differ from examples

Mon Feb 17, 2014 8:21 pm

Thanks, with version 2.7 examples I got everything to go as expected.

Should I change to Python 3 or could I just learn from older examples. Is there big diffence between the two?

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Printout results differ from examples

Mon Feb 17, 2014 8:53 pm

That's a can of worms. I don't believe the differences are big for learning purposes. About half the replies will say "Yes, meet the future, get used to 3", and the other half will say "Stick with what works, let someone else bleed on the edge for awhile". The problem is that some 'standard' modules/libraries have not been updated for 3, and some code will only work with 3. Me, I'm sticking with 2.7. Ya pays your money and ya takes your chance. You CAN (already do?) have both installed, and switch between them, so again, no great consequences either way.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Printout results differ from examples

Mon Feb 17, 2014 9:04 pm

Delzun wrote:Should I change to Python 3 or could I just learn from older examples. Is there big diffence between the two?
Personally (and I stand prepared for flak) I'd stick with Python 2.x while you get to grips with the language in general. There are far more examples and tutorials to look at than there are for Python 3 which, obviously, makes learning easier.
Once you're comfortable with the basics make the choice (or just do the right thing and stick with 2.x).

Dave.

ps. If they'd have just made the whole thing backward compatible things would have been a whole lot easier.
Apple say... Monkey do !!

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Printout results differ from examples

Mon Feb 17, 2014 9:09 pm

Dave just stated my opinion in the grand python flame wars better than I could.

I've yet to fathom the reasons for the incompatibility.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
croston
Posts: 708
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Printout results differ from examples

Mon Feb 17, 2014 9:31 pm

The major version number changed from 2 to 3 so some long term backwards incompatible improvements to the Python language could be made. If full compatibility was maintained, the 2.x series would have continued. Python 3 cleans up a few language inconsistencies:
[*] The print statement became a function so it could be overloaded
[*] The raw_input / input function was fixed for security reasons
[*] Integer division now behaves as expected - dividing integers produces a float. e.g 1/2 now returns 0.5 instead of 0 (a truncated integer)
[*] There are more changes but these are the ones that you will encounter as a beginner

These changes make Python 3 more beginner friendly. Python 2 is the past.

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Printout results differ from examples

Mon Feb 17, 2014 10:01 pm

croston wrote: These changes make Python 3 more beginner friendly. Python 2 is the past.
Are you able to explain why Raspbian still defaults to python 2.7 and haven't made python 3 the default interpreter. They aren't encouraging any of the novices to make the switch.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13142
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Printout results differ from examples

Mon Feb 17, 2014 10:03 pm

probably because most of the libraries are written for 2.x.

User avatar
croston
Posts: 708
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Printout results differ from examples

Mon Feb 17, 2014 10:24 pm

mahjongg wrote:probably because most of the libraries are written for 2.x.
Most libraries are now written for both. The ones that aren't don't seem to be actively maintained. The maintainers have had since 2008 to update their code! This does not include the time in alpha or beta either.

Return to “Python”