user12309212
user12309212

Reputation:

Unexpected output using print

I am using Python 3.6.8. on Ubuntu linux 18.04

I have started a tutorial with simple print statements. The code asks a question requiring input, then outputs the answer.

I have duplicated the last line in the code,as it illustrates the problem

#!/bin/python3

born = input('What year were you born?')
born = int(born)
age = 2025 - born
print(age)
print('In the year 2025 you will be', age, 'years old')
print 'In the year 2025 you will be', age, 'years old'

I expect the result from the first print statement to be; In the year 2025 you will be 75 years old

and the second should give a syntax error (as it is Python 3 and there are no brackets)

What I get is this;

('In the year 2025 you will be', 75, 'years old') In the year 2025 you will be 75 years old

Where is this going wrong?

Upvotes: 3

Views: 179

Answers (2)

WoleM
WoleM

Reputation: 11

Run python or python3 --version and see what you got installed, your script would run with any version.
Print with parenthesis works on 3+ while without would work on 2.

Upvotes: 1

user12309212
user12309212

Reputation:

Looking at all your suggestions has found the answer. I was not aware that both python2 and python3 both existed on the machine. Running python2 explicitly results in no errors. Running python3 explicitly results in the expected syntax error.

alex@Desktop:~/Python$ python3 -V
Python 3.6.8
alex@Desktop:~/Python$ python -V
Python 2.7.15+
alex@Desktop:~/Python$ python test.py
What year were you born?1972
53
('In the year 2025 you will be', 53, 'years old')
In the year 2025 you will be 53 years old
alex@Desktop:~/Python$ python3 test.py
  File "test.py", line 12
    print 'In the year 2025 you will be', age, 'years old'
                                       ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('In the year 2025 you will be', age, 'years old')?

Thanks to all respondents for their help

Upvotes: 0

Related Questions