所以,我有我的代码,它是为希望从图书馆或其他地方借书的用户编写的。他们输入他们想借的书的代码,我在文本文件中有指定名称、价格等的代码。他们输入他们想借出的书的数量,然后计算价格。如果他们想借更多的书,代码会循环回来,他们可以重复这个过程。我想创建一个单独的收据文件,该文件将用户在该收据文件的循环中输入的所有内容存储在一起,然后将所有内容添加到一起。我读了一些东西,并尝试了这个,但有一个错误,我不知道我做错了什么。这是我的密码:
task=input("Enter 'b' to borrow a book, press 'x' to exit. \n")
if task.lower()== "b":
myfile=open("books.txt", "r+")
details=myfile.readlines()
while True:
book=input("Enter the 8 digit book code. \n")
while len(book) !=8
print("Your code is not 8 digits long, please try again.\n")
f = open("books.txt", "r+")
for line in f.readlines():
quantity=input("How many copies of the book do you wish to purchase?\n")
t = line.split(" ")
price = float(t[3])
code = t[1]
NameOfBook = t[2]
total=(price)* int(quantity)
with open("receipt.txt", "w") as receiptFile:
receiptFile.write(str(code))
receiptFile.write(float(NameOfItem))
receiptFile.write(int(quantity))
receiptFile.write(str(price))
receiptFile.write(str(total))
break
answer = input("Do you want to loan another book? Enter 'yes' if you would like to. ")
if answer == "yes":
continue
else:
break
我试图进入的是:
with open("receipt.txt", "w") as receiptFile:
receiptFile.write(str(code))
receiptFile.write(float(NameOfBook))
receiptFile.write(int(quantity))
receiptFile.write(str(price))
receiptFile.write(str(total))
我假设它会打开名为receipt.txt的文件,然后写入用户输入的每一项内容,但最终没有成功。而是出现了这样的情况:
Traceback (most recent call last):
File "F:\PYTHOn\New python edited for loop.py", line 19, in <module>
receiptFile.write(float(NameOfBook))
ValueError: could not convert string to float: 'Narnia,'
如果可能的话,请帮忙。谢谢:)
目前没有回答
相关问题 更多 >
编程相关推荐