Python显示额外的第一个括号

Python显示额外的第一个括号,python,python-2.7,Python,Python 2.7,我已经开始学习使用Python编程。有一些数学练习,例如: print("Hens", 25 + 30 / 6) 通常,输出是hens30.0,但当我在Windows机器上运行它时,输出是('Hens',30)。为什么会这样 为清楚起见,我的代码是: print("I will now count my chickens:") print("Hens", 25 + 30 / 6) print("Roosters", 100 - 25 * 3 % 4) print("Now I will c

我已经开始学习使用Python编程。有一些数学练习,例如:

print("Hens", 25 + 30 / 6)
通常,输出是
hens30.0
,但当我在Windows机器上运行它时,输出是
('Hens',30)
。为什么会这样

为清楚起见,我的代码是:

print("I will now count my chickens:")

print("Hens", 25 + 30 / 6)
print("Roosters", 100 - 25 * 3 % 4)

print("Now I will count the eggs:")

print(3 + 2 + 1 - 5 + 4 % 2 - 1/ 4 + 6)

print("Is it true that 3 + 2 < 5 - 7?")

print(3 + 2 < 5 - 7)

print("What is 3 + 2?", 3 + 2)
print("What is 5 - 7?", 5 - 7)

print("Oh, that's why it's False.")

print("How about some more.")

print("Is it greater?", 5 > -2)
print("Is it greater or equal?", 5 >= -2)
print("Is it less or equal?", 5 <= -2)
问题是
()
,有人能给我指点一下吗


感谢在Python3中,Python2的
print
语句被更改为函数:
print(…)
。因此Python2将这个Python3语句解释为使用元组调用
print
语句

$ python2 -c 'print(1, 2, 3)'
(1, 2, 3)
$ python3 -c 'print(1, 2, 3)'
1 2 3


相关文档:.

在Python3中,Python2的
print
语句更改为函数:
print(…)
。因此Python2将这个Python3语句解释为使用元组调用
print
语句

$ python2 -c 'print(1, 2, 3)'
(1, 2, 3)
$ python3 -c 'print(1, 2, 3)'
1 2 3


相关文档:。

不同之处在于Python版本。 如果您使用的是Python 2.XX,那么在调用print函数时不需要括号;但是,在Python 3.XX中,必须使用带括号的print()

如果要解决问题,您应该更改Python版本,或者只需从打印函数中删除所有括号,如下所示:

print("I will now count my chickens:")

print "Hens", 25 + 30 / 6 
print "Roosters", 100 - 25 * 3 % 4

print("Now I will count the eggs:")

print 3 + 2 + 1 - 5 + 4 % 2 - 1/ 4 + 6

print("Is it true that 3 + 2 < 5 - 7?")

print 3 + 2 < 5 - 7

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print("Oh, that's why it's False.")

print("How about some more.")

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2
print(“我现在要数我的鸡:”)
打印“母鸡”,25+30/6
打印“公鸡”,100-25*3%4
打印(“现在我要数鸡蛋:”)
打印3+2+1-5+4%2-1/4+6
打印(“3+2<5-7是真的吗?”)
打印3+2<5-7
打印“什么是3+2?”,3+2
打印“什么是5-7?”,5-7
打印(“哦,这就是为什么它是假的。”)
打印(“再来点怎么样?”)
打印“是否更大?”,5>-2
打印“是否大于或等于?”,5>=-2

打印“它是小于还是等于?”,5不同之处在于Python版本。 如果您使用的是Python 2.XX,那么在调用print函数时不需要括号;但是,在Python 3.XX中,必须使用带括号的print()

如果要解决问题,您应该更改Python版本,或者只需从打印函数中删除所有括号,如下所示:

print("I will now count my chickens:")

print "Hens", 25 + 30 / 6 
print "Roosters", 100 - 25 * 3 % 4

print("Now I will count the eggs:")

print 3 + 2 + 1 - 5 + 4 % 2 - 1/ 4 + 6

print("Is it true that 3 + 2 < 5 - 7?")

print 3 + 2 < 5 - 7

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print("Oh, that's why it's False.")

print("How about some more.")

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2
print(“我现在要数我的鸡:”)
打印“母鸡”,25+30/6
打印“公鸡”,100-25*3%4
打印(“现在我要数鸡蛋:”)
打印3+2+1-5+4%2-1/4+6
打印(“3+2<5-7是真的吗?”)
打印3+2<5-7
打印“什么是3+2?”,3+2
打印“什么是5-7?”,5-7
打印(“哦,这就是为什么它是假的。”)
打印(“再来点怎么样?”)
打印“是否更大?”,5>-2
打印“是否大于或等于?”,5>=-2

print“it less or equal?”,5正如其他人所指出的,python2中的
print
调用调用语句,而python3中的调用函数。在Python3中必须添加括号,而在Python2中括号表示元组

在Python 2.6+中,您可以通过从
\uuuuuu future\uuuu
包导入print函数,从Python 3引入print函数的新行为:

from __future__ import print_function

这提供了Python3行为,并允许在Python2和Python3解释器下以类似的行为执行打印语句。(当然,其他特定于Python 2的构造可能需要单独处理。)

正如其他人所指出的,Python 2中的
print
调用调用语句,而Python 3中的调用函数。在Python3中必须添加括号,而在Python2中括号表示元组

在Python 2.6+中,您可以通过从
\uuuuuu future\uuuu
包导入print函数,从Python 3引入print函数的新行为:

from __future__ import print_function

这提供了Python3行为,并允许在Python2和Python3解释器下以类似的行为执行打印语句。(当然,其他特定于Python 2的构造可能需要单独处理。)

好的,现在我可以为这些输出做些什么,比如输出
$python3-c'print(1,2,3)'1 2 3
@jesica你是什么意思?您在错误的Python版本上运行代码!这是Python3代码,您正在版本
2.7
上运行它!好的,现在我能为这个输出做些什么,比如输出
$python3-c'print(1,2,3)'12 3
@jesica你是什么意思?您在错误的Python版本上运行代码!这是Python3代码,您正在版本
2.7
上运行它!