Metin dosyasındaki bir sözcüğün nasıl değiştirileceğini bilen var mı?Metin dosyasındaki bir kelimenin değiştirilmesi
İşte benim stok dosyasından bir satır var:
Ben tarafından oluşturulan yeni bir numara ile 'ProductLine' yazdırırken (bu örnekte '300' olarak) benim 4 sözcüğün yerine isterlerbread 0.99 12135479 300 200 400
Bu kodun nstock kısmı:
for line in details: #for every line in the file:
if digits in line: #if the barcode is in the line
productline=line #it stores the line as 'productline'
itemsplit=productline.split(' ') #seperates into different words
price=float(itemsplit[1]) #the price is the second part of the line
current=int(itemsplit[3]) #the current stock level is the third part of the line
quantity=int(input("How much of the product do you wish to purchase?\n"))
if quantity<current:
total=(price)*(quantity) #this works out the price
print("Your total spent on this product is:\n" + "£" +str(total)+"\n") #this tells the user, in total how much they have spent
with open("updatedstock.txt","w") as f:
f.writelines(productline) #writes the line with the product in
nstock=int(current-quantity) #the new stock level is the current level minus the quantity
yeni stok seviyesi (nstock)
Yani neler yaşadıklarını sorun veya hata? Bunu öğrenmek için kodu çalıştırmamızı istemek yerine, bunu belirtmelisiniz. –
Sadece yeni öğeler için 'itemsplit' sonra' '' .join (itemsplit) 'öğesinde doğru öğeye atayın. – cdarke
Sorunu – EmDuff