简体   繁体   English

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

[英]Trying to print an int but it comes up with : TypeError must be str, not float

I'm trying to get my program to print an int which follows a string. 我正在尝试让我的程序打印跟随字符串的int。 I'm not sure if that affects it or what but it comes up with the error whenever it tries to print. 我不确定这是否会影响它或什么,但是在尝试打印时会出现错误。

I've tried using "," instead but I don't want to as it outputs The Price Is £ 7.0 but it doesn't look as good as £7.0 我尝试使用“,”代替,但我不想这样做,因为它输出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)

price is a float, so it cannot be concatenated to a string. price是一个浮点数,因此不能将其串联为一个字符串。 Use string formatting instead: 请改用字符串格式:

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

For example: 例如:

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

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

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

Try this: 尝试这个:

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

Or 要么

print("The Price Is £",price)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM