简体   繁体   English

使用打印意外 output

[英]Unexpected output using print

I am using Python 3.6.8.我正在使用 Python 3.6.8。 on Ubuntu linux 18.04在 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到 2025 年,您将 75 岁

and the second should give a syntax error (as it is Python 3 and there are no brackets)第二个应该给出语法错误(因为它是 Python 3 并且没有括号)

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 ('2025 年你会', 75, '岁') 2025 年你会 75 岁

Where is this going wrong?这哪里出错了?

Run python or python3 --version and see what you got installed, your script would run with any version.运行pythonpython3 --version看看你安装了什么,你的脚本可以运行任何版本。
Print with parenthesis works on 3+ while without would work on 2 .带括号的打印适用于3+而没有括号的打印适用于2

Looking at all your suggestions has found the answer.查看您的所有建议已找到答案。 I was not aware that both python2 and python3 both existed on the machine.我不知道机器上都存在python2和python3。 Running python2 explicitly results in no errors.显式运行 python2 不会导致错误。 Running python3 explicitly results in the expected syntax error.显式运行 python3 会导致预期的语法错误。

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感谢所有受访者的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM