关于python的格式化输出
price = input("please input a number: ")
txt = "The price is {:.2f} dollars"
print(txt.format(price))
会报错:
ValueError: Unknown format code 'f' for object of type 'str'
但是改成这样:
price = int(input("please input a number: "))
txt = "The price is {:.2f} dollars"
print(txt.format(price))
即可运行,这里的int改成float什么的一样可以
这是为什么捏?