gpt4 book ai didi

python - print() 在结果中显示引号

转载 作者:太空宇宙 更新时间:2023-11-03 13:18:22 27 4
gpt4 key购买 nike

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

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!: ")

最佳答案

您需要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.

关于python - print() 在结果中显示引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22262379/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com