3

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?

6
  • 1
    Are you sure you have used python3 to ran this script?. Bcoz only python2 will print first as in tuple and second as string. Commented Nov 2, 2019 at 14:36
  • I tested it on my local Python 3.6.8 setup and it gives a syntax error before even taking inputs. Can you verify if your python3 points to python 2.7.* by any chance? Commented Nov 2, 2019 at 14:37
  • Hey @Alex , it seems that your program is working just fine, I tried it and it gave the expected result and also syntax error as no brackets were there in the last print statement Commented Nov 2, 2019 at 14:37
  • You expect python3 should give an error when there is no parenthesis, that's correct. But python2 can handle both print statements with or without parenthesis. So you are probably running the script using python2. Can you provide info how you run the script and what's the output of which python? Commented Nov 2, 2019 at 14:43
  • 1
    How do you run that file? If you run it with ./filename.py or python3 filename.py, it should work fine. But the output shows that you may have run the file using python filename.py and python is a symbolic link to python2 on many machines. Commented Nov 2, 2019 at 14:44

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.