2

Simple code but a a bit of an odd issue. Python tends to print the brackets and coma in the print function. This only happens in row 5 and row 7 but not for the last row. Any idea what's wrong?

e.g. output for each row:

(2016, is a leap year)

(2015, is not a leap year)

Invalid year.

year_str = input("Please enter a numerical year: ")
year = float(year_str)
if year == int(year) and year > 0:
    if (year/4)==int(year/4) and (year/100)!=int(year/100) or (year/400)==int(year/400):
        print(year_str, " is a leap year.")
    else:
        print(year_str, "is not a leap year.")
else:
    print("Invalid year.")
ohoh7171
  • 186
  • 2
  • 2
  • 14
  • 1
    Tell the version of Python: 2 or 3? And have a look at this question: https://stackoverflow.com/questions/6182964/why-is-parenthesis-in-print-voluntary-in-python-2-7 – vahdet Jul 09 '18 at 09:05
  • 3
    Are you using Python 2? If yes, then you're printing a tuple in the first 2 cases. If that's not what you want, you should check out string concatenation / formatting possibilities. – Mike Scotty Jul 09 '18 at 09:05
  • To solve your problem, I would use the .format() method available since python2.x https://docs.python.org/2/library/stdtypes.html#str.format – Florian Jul 09 '18 at 09:06
  • 4
    You could add this as your first line: `from __future__ import print_function` – quamrana Jul 09 '18 at 09:14

5 Answers5

4

Your problem is you're using code written for python 3, where print is a function:

>>> import sys; sys.version_info.major
3
>>> print('a', 'b')
a b

but running it in python 2, where it is a statement:

>>> import sys; sys.version_info.major
2
>>> print ('a', 'b')
('a', 'b')

If you're writing code that you want to print the same way in both python 2 and python 3, you can use

from __future__ import print_function
print('a', 'b')  # works as expected in both versions of python
Eric
  • 95,302
  • 53
  • 242
  • 374
2

use:

from __future__ import print_function

as the first line in your script. Then python 2.7 print is compatible with python 3 print

hootnot
  • 1,005
  • 8
  • 13
1

Since you're using python 2.7, print is a statement (not a function) so it takes no arguments and is called without parentheses (thanks to bruno for pointing this out in the comments). So in the first two print statements, you're just printing a tuple. In the last instance the parentheses are grouping a single element and so do nothing.

print('a', 'b') # print tuple
print ('a', 'b') # print tuple, but whitespace makes it more clear what's happening
print('a') # print string
print ('a') # print string, but whitespace makes it more clear what's happening
Jonas
  • 1,473
  • 2
  • 13
  • 28
  • 3
    "Only in the last instance do the parentheses actually act as parentheses for the print function." => wrong. In Python 2, `print` is a statement, and in the last example the parens are just grouping elements (which since there's a single element is actually a no-op). – bruno desthuilliers Jul 09 '18 at 09:09
0

Use correct formatting when printing text:

year_str = input("Please enter a numerical year: ")
year = float(year_str)
if year == int(year) and year > 0:
    if (year/4)==int(year/4) and (year/100)!=int(year/100) or (year/400)==int(year/400):
        print("%s is a leap year." % year_str)
    else:
        print("%s is not a leap year." % year_str)
else:
    print("Invalid year.")
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

print isn't a method in Python2, it's a statement. You should skip using parenthesis unless you want to print a tuple.

year_str = input("Please enter a numerical year: ")
    year = float(year_str)
    if year == int(year) and year > 0:
        if (year/4)==int(year/4) and (year/100)!=int(year/100) or (year/400)==int(year/400):
            print str(year_str) +" is a leap year."
        else:
            print str(year_str) +" is not a leap year."
    else:
        print "Invalid year."
Florian
  • 1,473
  • 10
  • 15
hondvryer
  • 442
  • 1
  • 3
  • 18
  • 3
    In python2 it's a statement, but in Python3 it's a function, so paranthesis *are* mandatory in *python3*. – Florian Jul 09 '18 at 09:08
  • Works fine with widely used versions both V2.7 and V3.6 – hondvryer Jul 09 '18 at 09:10
  • Python3 does not accept `print` as a statement. See : *The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105).* from : https://docs.python.org/3.0/whatsnew/3.0.html You can also try `print "test"` and launch your code with python3, it will raise a `SyntaxError: Missing parentheses in call to 'print'.` – Florian Jul 09 '18 at 09:15
  • But he is using python 2.7 so its fine. – hondvryer Jul 09 '18 at 09:20