【问题标题】:Can not concatenate float to string无法将浮点数连接到字符串
【发布时间】:2020-10-15 19:56:13
【问题描述】:
name = input ("What is your name? ")
age = input ("What is your age? ")
sleep = input ("How many hours do you sleep at night? ")

sleep1 = int(sleep)
age1 = int(age)

wasted_years = (sleep1/24) * age1
wasted_years1 = round(wasted_years, 2)

print("Hello "  + name)
print("You have been asleep unconscious for " + wasted_years1)

【问题讨论】:

  • 您需要显示“工作”代码。这不是一种语言。

标签: string concatenation


【解决方案1】:

您需要根据给定类型生成格式化,例如

print("You have been asleep unconscious for %.2f years." % (wasted_years1))

或者直接使用str() 将其转换为字符串并连接

print("You have been asleep unconscious for " + str(wasted_years1) + " years.")

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多