我正在使用Python3.6.8。Ubuntu 18.04
我用简单的打印语句开始了一个教程。代码提出一个需要输入的问题,然后输出答案。
我重复了代码中的最后一行,因为它说明了这个问题。
#!/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'我预计第一份打印声明的结果是:2025年你将75岁。
第二个应该给出一个语法错误(因为它是Python 3,没有括号)。
我得到的是这个;
(‘2025年你将是’‘,’75岁‘)在2025年你将是75岁
这是哪里出了问题?
发布于 2019-11-02 22:03:27
仔细考虑你的所有建议,就找到了答案。我不知道python2和python3都存在于机器上。显式运行python2不会导致错误。显式运行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')?感谢所有受访者的帮助
发布于 2019-11-02 14:50:53
运行python或python3 --version,看看安装了什么,您的脚本将与任何版本一起运行。
带括号的打印适用于3+,而不带括号的打印则适用于2。
https://stackoverflow.com/questions/58671996
复制相似问题