print()在结果中显示引号

时间:2014-03-07 23:01:25

标签: python python-2.7

当脚本的以下部分被激活时,它会在结果中显示所有逗号和单引号(和括号)。

print(name, 'has been alive for', days, 'days', minutes, 'minutes and', seconds, 'seconds!')

所以,例如:

('Ryan', 'has been alive for', 10220, 'days', 14726544, 'minutes and', 883593928, seconds!')

我想清理它,所以看起来不错。那可能吗?

“好”,我的意思是这样的:

Ryan has been alive for 10220 days, 14726544 minutes, and 883593928 seconds!

以下是完整的脚本:

print("Let's see how long you have lived in days, minutes, and seconds!")
name = raw_input("name: ")

print("now enter your age")
age = int(input("age: "))

days = age * 365
minutes = age * 525948
seconds = age * 31556926

print(name, 'has been alive for', days, 'days', minutes, 'minutes and', seconds, 'seconds!')

raw_input("Hit 'Enter' to exit!: ")

1 个答案:

答案 0 :(得分:7)

您需要from __future__ import print_function

在Python 2.x中,当你使用3.x语法时,你正在做的事情被解释为打印一个元组。

示例:

>>> name = 'Ryan'
>>> days = 3
>>> print(name, 'has been alive for', days, 'days.')
('Ryan', 'has been alive for', 3, 'days.')
>>> from __future__ import print_function
>>> print(name, 'has been alive for', days, 'days.', sep=', ')
Ryan, has been alive for, 3, days.
相关问题