Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Debug please
#5
(Jul-26-2021, 02:34 PM)deanhystad Wrote: Your main problem is that your code is a mess. You use global variables for passing information in and out of functions and you use recursion when you want to repeat code. That is always going to make bad code no matter what the underlying algorithm.

Your first step should be to eliminate all recursive function calls. Recursion has it's uses, but none of them are what you are using recursion for. If you want to input multiple items, use a loop. If you want to ask for information until the information is correct, use a loop. If you ever want to use recursion, use a loop. Maybe that last bit of advice is too harsh, but barely.

Your second step should be to eliminate all the global variables. Global variables make it too hard to write correct code. The main benefit of functions is they break code into little pieces. If each piece works, and you use the pieces correctly the program works (usually). If your functions use global variables, the pieces lose there autonomy. A change to piece "A" might break the code in piece "B", and the only way to spot the error is to look at the code in its entirety which defeats the purpose of using functions in the first place. If a function needs a value, pass in the value as a function argument. If the function generates information, pass the information back as a return.

Look at any repeated code. Why do you have a function to get the sending address and another function to get the receiving address? They both do the same thing. Modify the code so it can do both roles. You also have some duplication in the PDF writer that I don't understand at all. Why is it there?

Once your code is cleaned up a bit I think you will find your error is in how you build the label_list. When you input the items you treat label_list as a flat list that looks like this:
Output:
[item1_style, item1_color, item1_labels, item1_quantity, ... itemn_style, itemn_color, itemn_labels, itemn_quantity]
But when you print the labels you treat the label list like it only contains one item.
       pdf.cell(100, 10, txt = str(label_list[0]) + " " + str(label_list[1]), ln = 8, align = '')
The code would be simpler if the label list was a list of labels/items. label_list[0] should have all the information you need to print labels for that item (size, color, label count, item count) and label_list[1] should be the information for the next item. I would also dump the total_labels list. If you can calculate the number of labels from the item information, do the calculation when needed instead of creating another list that has to be synchronized with another list.

Hello,

The global variables could be downsized, I completely agree and I normally avoid using them. When the concept is working then I will tidy it up etc... I appreciate that currently it is a mess.

The duplication in the pdf code was only recently done. I'm new to python and to FPDF and I was originally trying to make the labels format as shown in my original post. upon failing this I left it as just the basic label format so I could change it later.

Thank you for your input though, I'll give it a clean up and see what works.

Cheers,
Jamie
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply


Messages In This Thread
Help Debug please - by jamesaarr - Jul-26-2021, 09:11 AM
RE: Help Debug please - by deanhystad - Jul-26-2021, 12:18 PM
RE: Help Debug please - by jamesaarr - Jul-26-2021, 12:38 PM
RE: Help Debug please - by deanhystad - Jul-26-2021, 02:34 PM
RE: Help Debug please - by jamesaarr - Jul-27-2021, 08:54 AM
RE: Help Debug please - by Pedroski55 - Jul-27-2021, 08:57 AM
RE: Help Debug please - by jamesaarr - Jul-27-2021, 09:15 AM
RE: Help Debug please - by jamesaarr - Jul-27-2021, 09:38 AM
RE: Help Debug please - by jamesaarr - Jul-27-2021, 09:58 AM
RE: Help Debug please - by Pedroski55 - Jul-27-2021, 11:20 AM
RE: Help Debug please - by Pedroski55 - Jul-28-2021, 02:52 AM
RE: Help Debug please - by jamesaarr - Jul-28-2021, 10:41 AM
RE: Help Debug please - by Pedroski55 - Jul-28-2021, 11:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  no debug messages going into log file robertkwild 0 630 Jul-09-2024, 05:30 PM
Last Post: robertkwild
  pycharm debug help mg24 1 1,859 Nov-18-2022, 05:38 AM
Last Post: deanhystad
  Python debug suddenly got bad ben1122 3 1,969 Sep-03-2022, 06:20 AM
Last Post: ben1122
  Error in Int object is not subscript-able. How to debug this ? yanDvator 1 2,900 Aug-03-2020, 02:28 PM
Last Post: Larz60+
  is there a debug mode available while creating python egg BhushanPathak 1 3,191 Dec-19-2018, 04:15 PM
Last Post: Larz60+
  Help debug my fibonacci implementation dineshpabbi10 6 5,448 May-16-2018, 12:12 PM
Last Post: dineshpabbi10
  Not sure how to debug this? rsmldmv 3 6,483 Nov-09-2017, 02:51 AM
Last Post: snippsat
  Debug and trace in python quocchi22101262 0 3,996 Jun-20-2017, 09:35 AM
Last Post: quocchi22101262

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020