Help with numberofitemshired

The problem is in the def check_validity section, the code is working perfectly except for the numberOfItemsHired bit. The goal is to try and get it to only accept values between 1-500 and if the user enters anything out of range an error message should display, however that part is not working.
Would you possibly know any way to solve this? I’ve tried different strategies as well but none of them have worked.

from tkinter import*
import tkinter as tk

def quit():
    main_window.destroy()

def print_details():
    global counter
    counter= 0
    Label(main_window, text= "Row").grid(column= 0, row= 7)
    Label(main_window, text= "Customer Name").grid(column= 1, row= 7)
    Label(main_window, text= "Receipt Number").grid(column= 2, row= 7)
    Label(main_window, text= "Item Hired").grid(column= 3, row= 7)
    Label(main_window, text= "Number of Item Hired").grid(column= 4, row= 7)
    ROWS_ABOVE= 8

    while counter < number['total_entries']:
        Label(main_window, text= counter).grid(column= 0, row= counter + ROWS_ABOVE)
        Label(main_window, text= (hireDetails[counter][0])).grid(column= 1, row= counter + ROWS_ABOVE)
        Label(main_window, text= (hireDetails[counter][1])).grid(column= 2, row= counter + ROWS_ABOVE)
        Label(main_window, text= (hireDetails[counter][2])).grid(column= 3, row= counter + ROWS_ABOVE)
        Label(main_window, text= (hireDetails[counter][3])).grid(column= 4, row= counter + ROWS_ABOVE)
        counter += 1
        number['counter']= counter

        

def check_validity():
    global items
    items= ["Spoons", "Forks" , "Knives" , "Tables" , "Chairs" , "Cater Dishes"]
    valid= 0

    if(entry_customer_name.get().isalpha()) and (entry_receipt_number.get().isnumeric()) and (entry_numberOfItemsHired.get().isdigit()):
       if (range(entry_receipt_number.get())) in range (1,99999999):
           valid= 1
       else:
            Label(main_window, text= "Receipt Number is invalid. Only 8 digits needed.", bg= 'red').grid(column= 2, row= 2, sticky= "W")
            if(range(int(entry_numberOfItemsHired.get()))) in range (1,500):
                valid= 1
            else:
                Label(main_window, text= "Only 1-500 items allowed to be hired.", bg= 'red').grid(column= 2, row= 4, sticky= "W")

                if (entry_itemHired.get()) in items:
                   valid= 1
                else:
                   Label(main_window, text= "Choose Item Hired: Spoons, Forks, Knives, Tables, Chairs, Cater Dishes.", bg= 'red').grid(column= 2, row= 3, sticky= "W")
    
    else:
        if len(entry_customer_name.get()) == 0:
            Label(main_window, text= "Enter Customer Name. No numbers allowed.", bg= 'red').grid(column= 2, row= 1, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 1, sticky= "W")                                        
            if not(entry_customer_name.get().isalpha()):
                Label(main_window, text= "No numbers allowed.", bg= 'red').grid(column= 2, row= 1, sticky= "W")
            
        if len(entry_receipt_number.get()) == 0:
            Label(main_window, text= "Receipt Number is invalid. Use digits.", bg= 'red').grid(column= 2, row= 2, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 2, sticky= "W")
            if not(entry_receipt_number.get().isnumeric()):
                Label(main_window, text= "Receipt Number is invalid. Use digits.", bg= 'red').grid(column= 2, row= 2, sticky= "W")
            elif len(range(int(entry_receipt_number.get()))) not in range (1,99999999):
                Label(main_window, text= "Receipt Number is invalid. Only 8 digits needed.", bg= 'red').grid(column= 2, row= 2, sticky= "W")
            
        if len(entry_itemHired.get()) == 0:
            Label(main_window, text= "Choose Item Hired: Spoons, Forks, Knives, Tables, Chairs, Cater Dishes.", bg= 'red').grid(column= 2, row= 3, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 3, sticky= "W")
            if (entry_itemHired.get()) not in items:
                Label(main_window, text= "Choose Item Hired: Spoons, Forks, Knives, Tables, Chairs, Cater Dishes.", bg= 'red').grid(column= 2, row= 3, sticky= "W")

        if len(entry_numberOfItemsHired.get()) == 0:
            Label(main_window, text= "Only 1-500 items allowed to be hired.", bg= 'red').grid(column= 2, row= 4, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 4, sticky= "W")
            if not(entry_numberOfItemsHired.get().isdigit()):
                Label(main_window, text= "Only 1-500 items allowed to be hired. Use digits.", bg= 'red').grid(column= 2, row= 4, sticky= "W")
            else:
                Label(main_window, text= "                                                           ").grid(column= 2, row= 4, sticky= "W")
        if not(range(int(entry_numberOfItemsHired.get()))) in range (1,500):
            Label(main_window, text= "Only 1-500 items allowed to be hired.", bg= 'red').grid(column= 2, row= 4, sticky= "W")

    valid= 0
    
    if valid == 1:
        append_details()
        Label(main_window, text= "Customer Name: ").grid(column= 0, row= 1)
        Label(main_window, text= "Receipt No.: ").grid(column= 0, row= 2)
        Label(main_window, text= "Item Hired: ").grid(column= 0, row= 3)
        Label(main_window, text= "No. of Items Hired: ").grid(column= 0, row= 4)
        Label(main_window, text= "                                                                      ").grid(column= 2, row= 1, sticky= "W")
        Label(main_window, text= "                                                                      ").grid(column= 2, row= 2, sticky= "W")
        Label(main_window, text= "                                                                      ").grid(column= 2, row= 3, sticky= "W")
        Label(main_window, text= "                                                                      ").grid(column= 2, row= 4, sticky= "W")

                       
def append_details():
    hireDetails.append([entry_customer_name.get(),entry_receipt_number.get(),entry_itemHired.get(),entry_numberOfItemsHired.get()])
    entry_customer_name.delete(0,'end')
    entry_receipt_number.delete(0,'end')
    entry_itemHired.delete(0,'end')
    entry_numberOfItemsHired.delete(0,'end')
    number ['total_entries'] += 1

def delete_row():
    del hireDetails [int(delete_details.get())-0]
    counter= number['counter']
    number['total_entries']-=1
    delete_details.delete(0,'end')
    Label(main_window, text= "                ").grid(column= 0, row= counter + 7)
    Label(main_window, text= "                ").grid(column= 1, row= counter + 7)
    Label(main_window, text= "                ").grid(column= 2, row= counter + 7)
    Label(main_window, text= "                ").grid(column= 3, row= counter + 7)
    Label(main_window, text= "                ").grid(column= 4, row= counter + 7)
    print_details()

def main():
    Button(main_window, text= "Quit", command= quit).grid(column= 4, row= 1)
    Button(main_window, text= "Print Details", command= print_details).grid(column= 4, row= 2)
    Button(main_window, text= "Append Details", command= check_validity).grid(column= 3, row= 2)
    Button(main_window, text= "Delete ", command= delete_row).grid(column= 4, row= 5)
    Label(main_window, text= "Customer Name: ").grid(column= 0, row= 1)
    Label(main_window, text= "Receipt No.: ").grid(column= 0, row= 2)
    Label(main_window, text= "Item Hired: ").grid(column= 0, row= 3)
    Label(main_window, text= "No. Of Items Hired: ").grid(column= 0, row= 4)
    Label(main_window, text= "Row No. : ").grid(column= 2 , row= 5)

    main_window.wm_title("Party Hire Shop - Julies")

    main_window.mainloop()


number= {'total_entries':0,'counter':0}
hireDetails= []
main_window= tk.Tk()
entry_customer_name= Entry(main_window)
entry_customer_name.grid(column= 1, row= 1)
entry_receipt_number= Entry(main_window)
entry_receipt_number.grid(column= 1, row= 2)
entry_itemHired= Entry(main_window)
entry_itemHired.grid(column= 1, row= 3)
entry_numberOfItemsHired= Entry(main_window)
entry_numberOfItemsHired.grid(column= 1, row=4)
delete_details= Entry(main_window)
delete_details.grid(column= 3, row= 5)


main()

Hi,

when your script reads in the value, it is being read in as a type str string by default. You have to convert it to an integer.

In the following line:

if (range(entry_receipt_number.get())) in range(1, 99999999):

Replace it with:

if (int(entry_receipt_number.get())) in range(1, 99999999):

You have to do the same for all entries where you’re expecting integer values. On the other hand, if you’re expecting a float value, you would prefix it with float.

Just a word of advice since there is always bound to be entry errors by users, I would recommend using try / exception exception handling (i.e., mistakes / errors) pair statements ensuring that the user enters the correct input type. For example, if the user mistakenly entered an actual str type together with an integer value, say by accidently typing a letter key when entering a number, it will crash the program if not handled in your script.

Yup, thanks heaps I will give that a go. I also need advice on the "if not(range(int(entry_numberOfItemsHired.get()))) in range (1,500): "bit. The program is not reading if the user input is within that range or not and I have no clue what the problem is. I have tried the try except method with it but it seems to not work as well.

Here is a simple example that highlights an alternative:

a = 562

if not(a >= 1 and a <= 500):
    print('It is not in range.')
else:
    print('Yes, it is in range.')

So, you can try:

if not(int(entry_receipt_number.get()) >= 1 and int(entry_receipt_number.get()) <= 500):

The mistake also lies in the fact that you’re prefixing your value with the keyword range. Why?

It should work perfectly fine if you remove it such that:

if (int(entry_receipt_number.get())) in range(1, 99999999):

I have applied the example in my code, however it is still not functioning well. I think it might be the indentation or lay out:

 if len(entry_numberOfItemsHired.get()) == 0 or not(entry_numberOfItemsHired.get().isdigit()): 
            Label(main_window, text= "Only 1-500 items allowed to be hired. Use Digits", bg= 'red').grid(column= 2, row= 4, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 4, sticky= "W")
            if len(range(int(entry_numberOfItemsHired.get()))) not in range (1,501):
                Label(main_window, text= "Only 1-500 items allowed to be hired.", bg= 'red').grid(column= 2, row= 4, sticky= "W")

Like I stated in my previous post, if you are expecting an integer value, you have to convert the input string to an integer type via the int prefix. In the above comparison, you are asking if the str type is equal to 0, which does not make sense (will always be False regardless of the value).

If you want to be certain, perform the following test:

print(type(entry_numberOfItemsHired.get()))

You should get <class 'str'>.

Perform this simple test:

user_input = input('Enter a value between 1 and 50 : ')
print(type(user_input))

This shows that by default, the numerical values that you enter are type str string by default. You have to tell the program to convert the numerical strings to integer values. You have to do this explicitly with the int keyword:

user_input = int(input('Enter a value between 1 and 50 : '))
print(type(user_input))

Hello,

I have tried this in my code, taking your advice into consideration but I am unsure of how to implement it, so the code is still unable to work.

def check_validity():  #make it so numberOfItemsHired is converted into string 
    global items
    items= ["Spoons", "Forks" , "Knives" , "Tables" , "Chairs" , "Cater Dishes"]
    valid= 0

    if(entry_customer_name.get().isalpha()) and (entry_receipt_number.get().isnumeric()) and (entry_itemHired.get()) and (entry_numberOfItemsHired.get().isdigit()) != 0:
        if (int(entry_receipt_number.get())) in range (1,99999999):
            valid= 1
        else:
           Label(main_window, text= "Receipt Number is invalid. Only 8 digits needed.", bg= 'red').grid(column= 2, row= 2, sticky= "W")
           if (entry_itemHired.get()) in items:
               valid= 1
           else:
               Label(main_window, text= "Choose Item Hired: Spoons, Forks, Knives, Tables, Chairs, Cater Dishes.", bg= 'red').grid(column= 2, row= 3, sticky= "W")

               if (entry_numberOfItemsHired.get().isdigit()) == True:
                   numberOfItems= int(entry_numberOfItemsHired.get())
                   if numberOfItems in range (1,501) == True:
                       valid= 1


    else:
        if len(entry_customer_name.get()) == 0:
            Label(main_window, text= "Enter Customer Name. No numbers allowed.", bg= 'red').grid(column= 2, row= 1, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 1, sticky= "W")                                        
            if not(entry_customer_name.get().isalpha()):
                Label(main_window, text= "No numbers allowed.", bg= 'red').grid(column= 2, row= 1, sticky= "W")

            
        if len(entry_receipt_number.get()) == 0:
            Label(main_window, text= "Receipt Number is invalid. Use digits.", bg= 'red').grid(column= 2, row= 2, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 2, sticky= "W")
            if not(entry_receipt_number.get().isnumeric()):
                Label(main_window, text= "Receipt Number is invalid. Use digits.", bg= 'red').grid(column= 2, row= 2, sticky= "W")
            elif (int(entry_receipt_number.get())) not in range (1,99999999):#works but needs enhancement
                Label(main_window, text= "Receipt Number is invalid. Only 8 digits needed.", bg= 'red').grid(column= 2, row= 2, sticky= "W")

            
        if len(entry_itemHired.get()) == 0:
            Label(main_window, text= "Choose Item Hired: Spoons, Forks, Knives, Tables, Chairs, Cater Dishes.", bg= 'red').grid(column= 2, row= 3, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 3, sticky= "W")
            if (entry_itemHired.get()) not in items:
                Label(main_window, text= "Choose Item Hired: Spoons, Forks, Knives, Tables, Chairs, Cater Dishes.", bg= 'red').grid(column= 2, row= 3, sticky= "W")

        
        if (entry_numberOfItemsHired.get().isdigit()) == False:
            Label(main_window, text= "Only 1-500 items allowed to be hired. Use Digits", bg= 'red').grid(column= 2, row= 4, sticky= "W")
        else:
            Label(main_window, text= "                                                           ").grid(column= 2, row= 4, sticky= "W") 
            numberOfItems= int(entry_numberOfItemsHired.get())#code skips to this line/not transforming this into integer
            if numberOfItems in range (1,501) == False:
                Label(main_window, text= "Only 1-500 items allowed to be hired.", bg= 'red').grid(column= 2, row= 4, sticky= "W")
                valid= 0
            else:
                Label(main_window, text= "                                                           ").grid(column= 2, row= 4, sticky= "W")
            valid= 0
    print(type(entry_numberOfItemsHired.get()))
    print(type(entry_receipt_number.get()))

    if valid == 1:
        append_details()
        Label(main_window, text= "Customer Name: ").grid(column= 0, row= 1)
        Label(main_window, text= "Receipt No.: ").grid(column= 0, row= 2)
        Label(main_window, text= "Item Hired: ").grid(column= 0, row= 3)
        Label(main_window, text= "No. of Items Hired: ").grid(column= 0, row= 4)
        Label(main_window, text= "                                                                      ").grid(column= 2, row= 1, sticky= "W")
        Label(main_window, text= "                                                                      ").grid(column= 2, row= 2, sticky= "W")
        Label(main_window, text= "                                                                      ").grid(column= 2, row= 3, sticky= "W")
        Label(main_window, text= "                                                                      ").grid(column= 2, row= 4, sticky= "W")

                       

Hi,

quick example. In your code, you have this line that is located about one fifth into your script:

if numberOfItems in range (1,501) == True:

That is an invalid line. It should be:

if numberOfItems in range (1,501):  # This should be enough for what you need

Here is a quick test to bring this point home:

numberOfItems = 588 # Then in next test, change this value to 25
                    # testing when it is in range and out of the range
if numberOfItems in range(1, 501): 
    print('Yes, the value is in the range.')
else:
    print('No, the value is not in the range.')

I would recommend to put this code aside. Create another module and slowly start pasting a maximum of five lines of your script at a time. Then proceed by testing each added section carefully until you fully understand the behavior of the added code. As of right now, you are attempting to debug a roughly ~90ish lines of code. The right way is to add a few lines → test → verify → debug if need be, → understand → then move on.

Here is another example:

    if (entry_customer_name.get().isalpha()) and (entry_receipt_number.get().isnumeric()) and (
    entry_itemHired.get()) and (entry_numberOfItemsHired.get().isdigit()) != 0:

There are unnecessary parenthesis that are being added.

You can just have:

    if entry_customer_name.get().isalpha() and entry_receipt_number.get().isnumeric() and entry_itemHired.get() and entry_numberOfItemsHired.get().isdigit() != 0:

If at any time you are unsure how a piece of code works, test it in isolation away from your application. Open another module and test the few lines there. Try different values. Print out the results.