如何让我的循环写入文件,直到我停止循环?
例如
outFile = "ExampleFile.txt", "w"
example = raw_input(" enter number. Negative to stop ")
while example >= 0:
example = raw_input("enter number. Negative to stop")
outFile.write("The number is", example,+ "\n")
我觉得我很接近它,但我不确定。我不知道如何特别搜索这个问题。抱歉,当我输入超过 2 个参数时,我不断收到错误消息,指出该函数需要 1 个参数。
import os.path
outFile = open("purchases.txt","w")
quantity = float(raw_input("What is the quantity of the item :"))
cost = float(raw_input("How much is each item :"))
while quantity and cost >= 0:
quantity = float(raw_input("What is the quantity of the item :"))
cost = float(raw_input("How much is each item :"))
total = quantity * cost
outFile.write("The quantity is %s\n"%(quantity))
outFile.write("the cost of the previous quality is $s\n" %(cost))
outFile.close()
outFile = open("purchases.txt","a")
outFile.write("The total is ",total)
outFile.close()