尝试打印一个int,但它附带了:TypeError必须是str,而不是float

时间:2019-05-08 09:01:12

标签: python python-3.x

我正试图让我的程序打印一个跟随字符串的int。我不确定这是否会影响它或影响什么,但是每次尝试打印时都会出现错误。

我尝试使用“,”代替,但是我不想使用它,因为它输出The Price Is £ 7.0,但看起来不如£7.0

price = 10
age = int(input("How Old Are You? - "))
if age > 12 and age < 14:
  price = price * 0.7

print("The Price Is £"+price)

3 个答案:

答案 0 :(得分:1)

price是一个浮点数,因此无法将其连接为字符串。请改用字符串格式:

print(f"The Price Is {price}£")

例如:

>>> price = 0.7
>>> print(f"The Price Is {price}£")
The Price Is 0.7£

答案 1 :(得分:1)

发生这种情况是因为您必须将int连接起来:

print("The Price Is £"+str(price))

答案 2 :(得分:0)

尝试一下:

print("The Price Is {}£".format(price))

print("The Price Is £",price)
相关问题