【发布时间】:2019-10-16 06:40:50
【问题描述】:
我是 Python3 的新手,我已经开始研究一个程序。这是一个金融计算器。我正在使用浮动数字(美元和美分)。我有一个单独的 .txt 文档 (history.txt),程序在整个代码中都引用了该文档。首先要做的事情之一是拿走用户的钱并将其保存到文本文件中。
我尝试使用 file.write() 命令,但它说参数必须是 str,而不是 float。
import os
cash = float(input("How much cash did you earn?"))
history = open("history.txt","w+")
if os.stat("history.txt").st_size == 0:
history.write(cash)
这是我收到的错误。
TypeError: write() 参数必须是 str,而不是 float
【问题讨论】:
-
要转换为字符串,只需使用
str。
标签: python python-3.x