1

I have a problem with python code and i don't know what to do, because im fairly new in it.

date_now1 = datetime.datetime.now()
archive_date1 = date_now1.strftime("%d.%m.%Y")
f1 = open(archive_date1, "r+")

print("What product do you wish to delete ?")
delate_product = str(input())
for line in f1.readlines():
    if delate_product in line:
        list = line
        ready_product = list.split()
        quantity_of_product = int(ready_product[1])
        if quantity_of_product == 1:
            del line
            print("Product deleted")
        else:
            print("You have {} amounts of this product. How many do you want to delete ?".format(quantity_of_product))
            x = int(input())
            quantity_of_product = quantity_of_product - x
            temporary = "{}".format(quantity_of_product)
            print(type(temporary))
            f1.write(temporary) in ready_product[1]

I get the message

    f1.write(temporary) in ready_product[1] 
TypeError: 'in <string>' requires string as left operand, not int

When i do print(type()) in temporary it says string. I also tried str(quantity_of_product), but it doesn't work as well. Maybe somebody could give me the idea of what to do, or what to read to get the answer.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • The error message is correct, as `f1.write(x)` is a function and returns an int. But ... what is that line supposed to do? What it *tries* to do is executing a single statement `a in b`, not even assigning the result to anything. So even if you got rid of that error message, it's still a nonsensical line. – Jongware Apr 21 '18 at 12:08
  • What im trying to do is to update quantity in line where the product is in file, after i delete certaint amount of some product. Do so the file would be used as a archive of my grocery shoping – Krystian Dorsz Apr 21 '18 at 12:19
  • 1
    A file does not support such `a in b` notation – now you explained it, I can see why you thought it *might*. But Python is not an "English parser", it's a regular programming language with preset rules on what syntax you can use when and where. Also, *writing* lines while you are still *reading* from that same file can only end in tears. Read the entire file, make changes, write back. – Jongware Apr 21 '18 at 12:25

1 Answers1

2

The error is arising because you are asking python to find out whether an integer is "in" a string.

The output of f1.write(temporary) is an integer. To see this, try adding a print statement before the erroneous line. In contrast, ready_product[1] is a string (i.e. the second string element in the list "ready_product").

The operator "in" takes two iterables and returns whether the first is "in" the second. For example:

>>> "hello in ["hello", "world"]
>> True
>>> "b" in "a string"
>> False

When Python attempts to see if an integer is "in" a string, it cannot and throws a TypeError, saying "requires string as left operand, not int". This is the root of your error.

You may also have a number of other errors in your code:

  • "list" is a reserved word in Python, and so calling your variable "list" is bad practice. Try another name such as _list (or delete the variable as it doesn't appear to serve a purpose).
  • "del line" deletes the variable "line". However, it won't delete the actual line in the text file, only the variable containing it. See Deleting a specific line in a file (python) for how to delete a line from a text file.
  • There doesn't appear to be a f1.close() statement in the code. This is necessary to close the file after use, as otherwise edits may not be saved.

Personally, instead of attempting to delete lines as I go, I'd maintain a list of lines in the text file, and delete/alter lines from the list as I go. Then, at the end of the program I'd rewrite the file from the list of altered lines.