我试图转换其值改变的变量,但是该值通常是一位小数,例如0.5。我试图将此变量更改为0.50。我使用这个代码,但是当我运行该程序,它说TypeError: Can't convert 'float' object to str implicitly
将浮点型变量转换为字符串
这里是我的代码:
while topup == 2:
credit = credit + 0.5
credit = str(credit)
credit = '%.2f' % credit
print("You now have this much credit £", credit)
vending(credit)
这不是我得到的错误。你使用的是什么版本的Python? – Kevin 2014-10-09 17:45:39
错误实际上是你在''%.2f'%credit'中传递一个字符串TypeError:需要一个float,并且需要'TypeError:float参数,而不是str',用于python 2. – 2014-10-09 17:49:24
@凯文我正在使用Python 3.4 – NoobProgrammer 2014-10-09 17:53:42