Reputation: 685
I've started to learn Python programming using Learn Python the Hard Way. Exercise 3 has some math practice, for example:
print("Hens", 25 + 30 / 6)
Normally, the output is Hens 30.0
but when I run it on my Windows machine the output is ('Hens', 30)
. Why is this?
For clarity, my code is:
print("I will now count my chickens:")
print("Hens", 25 + 30 / 6)
print("Roosters", 100 - 25 * 3 % 4)
print("Now I will count the eggs:")
print(3 + 2 + 1 - 5 + 4 % 2 - 1/ 4 + 6)
print("Is it true that 3 + 2 < 5 - 7?")
print(3 + 2 < 5 - 7)
print("What is 3 + 2?", 3 + 2)
print("What is 5 - 7?", 5 - 7)
print("Oh, that's why it's False.")
print("How about some more.")
print("Is it greater?", 5 > -2)
print("Is it greater or equal?", 5 >= -2)
print("Is it less or equal?", 5 <= -2)
and the output is
$ python ex2.py
I will now count my chickens:
('Hens', 30)
('Roosters', 97)
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
('What is 3 + 2?', 5)
('What is 5 - 7?', -2)
Oh, that's why it's False.
How about some more.
('Is it greater?', True)
('Is it greater or equal?', True)
('Is it less or equal?', False)
Python version:
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
The issue is ()
, can anyone guide me about that, please.
Thanks
Upvotes: 0
Views: 59
Reputation: 5379
As others have identified, your print
calls in Python 2 call a statement, whereas in Python 3, they call a function. The addition of brackets is necessary in Python 3 whereas they indicate a tuple in Python 2.
In Python 2.6+, you can bring in the new behaviour of the print function from Python 3 by importing the print function from the __future__
package:
from __future__ import print_function
This provides the Python 3 behaviour and will allow your print statements to be executed with similar behaviour under both Python 2 and 3 interpreters. (Other Python 2-specific constructs may, of course, require separate treatment.)
Upvotes: 1
Reputation: 354
The difference is in the version of Python. If you're using Python 2.XX you don't need the brackets when you are calling the print function; however in Python 3.XX you must use print() with the brackets.
If you want to solve your problem, you should change your Python version or just remove every brackets from the print function like this:
print("I will now count my chickens:")
print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4
print("Now I will count the eggs:")
print 3 + 2 + 1 - 5 + 4 % 2 - 1/ 4 + 6
print("Is it true that 3 + 2 < 5 - 7?")
print 3 + 2 < 5 - 7
print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7
print("Oh, that's why it's False.")
print("How about some more.")
print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2
Upvotes: 1
Reputation: 20414
In Python3, the print
statement of Python2 was changed to function: print(...)
. So Python2 interprets this Python3 statements as calling the print
statement with a tuple.
$ python2 -c 'print(1, 2, 3)'
(1, 2, 3)
$ python3 -c 'print(1, 2, 3)'
1 2 3
Relevant documentation: https://docs.python.org/3/whatsnew/3.0.html#print-is-a-function.
Upvotes: 4