Home > OS >  Why wont "if list[i] == itemNo "\n":" work?
Why wont "if list[i] == itemNo "\n":" work?

Time:04-04

I got some problem with my code as you can see and I have no idea how to fix it. Even I did a lot of research. So the terminal tells me:

    if list[i] == itemNo   "\n":
    TypeError: unsupported operand type(s) for  : 'int' and 'str'

I am going to put the whole code in because I have no idea what you need to fix my problem.

from tkinter import *
from tkinter import messagebox

class Vend(Button):
    def vend(self):
        root = Tk()
        root.geometry("500x400")
        root.title("Sell vehicle")
        frame1 = Frame(root, relief="ridge", borderwidth=5)
        frame1.pack(fill="both", expand=1)

        labelMain = Label(frame1, text="Which car do you want to sell?")
        labelMain.config(font=("Arial", 14, "bold"))
        labelMain.place(x=50, y=10)
        label1 = Label(frame1, text="Item number:")
        label1.place(x=100, y=100)

        input1 = Entry(frame1, bd=2, width=22)
        input1.place(x=200, y=100)

        class MyButton(Button):
            def action1(self):
                try:
                    itemNo = int(input1.get())
                except ValueError:
                    messagebox.showerror('ERRORx03:"ValueError"', "The item number must be an even number")
                else:
                    f = open(r"C:\Users\triss\Desktop\Desktop\Programmieren\Python lernen\P.21\Dateien\sortiment.txt", "r")
                    list = f.readlines()
                    f.close()
                    print(list)

                    sold = False
                
                    for i in range(len(list)):
                        if list[i] == itemNo "\n":
                            f = open(r"C:\Users\triss\Desktop\Desktop\Programmieren\Python lernen\P.21\Dateien\sortiment.txt", "w")
                            list = list[:i] list[i 5:]
                            for line in list:
                                f.write(line)
                            sold = True
                            break
                    if sold:
                        messagebox.showinfo("Success!", "The car was successfully sold!")
                        root.destroy()
                    else:
                        messagebox.showerror("ERRORx01", "You have not entered a valid item number!\nPlease try again.")

        button = MyButton(frame1, text="sell")
        button["command"] = button.action1
        button.place(x=180, y=220)

        root.mainloop

CodePudding user response:

You need to convert itemNo to string first (if it was an integer) because integer string is impossible. So, the solution would be:

if list[i] == str(itemNo) "\n":

CodePudding user response:

As j1-lee and DeepSpace were saying, you cannot add the integer and the text together. I agree with j1 and suggest using the str() operator (for example if list[i] == str(itemNo "\n"): iirc). It also looks like Syumza just posted an answer and I think that'd work also.

EDIT: I think I did get it wrong, Syumza's answer is the way to go.

  • Related