Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:48:57+00:00 2026-05-27T04:48:57+00:00

Considering this is only for my homework I don’t expect much help but I

  • 0

Considering this is only for my homework I don’t expect much help but I just can’t figure this out and honestly I can’t get my head around what’s going wrong. Usually I have an idea where the problem is but now I just don’t get it.

Long story short: I’m trying to create a valid looking telephone number within a class and then loading it onto an array or list then later on save all of them as string into a folder. When I start the program again I want it to read the file and re-create my class and load it back into the list. (Basically a very simple repository).

Problem is even though I evaluate the stored phone number in the exact same way I validate it as input data … I get an error which makes no sens.

Another small problem is the fact that when I re-use the data for some reason it creates white spaces in the file which in turn messes my program up badly.

Here I validate phone numbers:

def validateTel(call_ID):
    if isinstance (call_ID, str) == True:
        call_ID = call_ID.replace (" ", "")
        if (len (call_ID) != 10):
            print ("Telephone numbers are 10 digits long")
            return False
        for item in call_ID:
            try:
                int(item)
            except:
                print ("Telephone numbers should contain non-negative digits")
                return False
            else:
                if (int(item) < 0):
                print ("Digits are non-negative")

After this I use it and other non-relevant (to this discussion) data to create an object (class instance) and move them to a list.

Inside my class I have a load from string and a load to string. What they do is take everything from my class object so I can write it to a file using "+" as a separator so I can use string.split("+") and write it to a file. This works nicely, but when I read it … well it’s not working.

    def load_data():
        f = open ("data.txt", "r")
        ch = f.read()
        contact = agenda.contact () # class object
        if ch in (""," ","None"," None"):
           f.close()
           return []  # if the file is empty or has None in some way I pass an empty stack
        else:
           stack = [] # the list where I load all my class objects
        f.seek(0,0)
        for line in f:
            contact.loadFromString(line) # explained bellow
            stack.append(deepcopy(contact))
        f.close()
        return stack

In loadFromString(line) all I do is validate the line (see if the data inside it at least looks OK).

Now here is the place where I validate the string I just read from the file:

def validateString (load_string):
    string = string.split("+")
    if len (string) != 4:
        print ("System error in loading from file: Program skipping segment of corrupt data")
        return False
    if string[0] == "" or string[0] == " " or string[0] == None or string[0] == "None" or string[0] == " None":
        print ("System error in loading from file: Name field cannot be empty")
    try:
        int(string[1])
    except:
        print("System error in loading from file: ID is not integer")
        return False
    if (validateTel(str(string[2])) == False):
        print ("System error in loading from file: Call ID (telephone number)")
        return False
    return True

Small recap:
I try to load the data from file using loadFromString(). The only relevant thing that does is it tries to validate my data with validateString(string) in there the only thing that messes me up is the validateTel. But my input data gets validated in the same way my stored data does. They are perfectly identical but it gives a “System error” BUT to give such an error it should have also gave an error in the validate sub-program but it doesn’t.

I hope this is enough info because my program is kinda big (for me any way) however the bug should be here somewhere.

I thank anyone brave enough to sift trough this mess.

EDIT:

The class is very simple, it looks like this:

    class contact:
         def __init__ (self, name = None, ID = None, tel = None, address = None):
             self.__name = name
             self.__id = ID
             self.__tel = tel
             self.__address = address

After this I have a series of setters and getters (to modify contacts and to return parts of the abstract data)

Here I also have my loadFromString and loadToString but those work just fine (except maybe they cause a small jump after each line (an empty line) which then kills my program, but that I can deal with)

My problem is somewhere in the validate or a way the repository interacts with it. The point is that even if it gives an error in the loading of the data, first the validate should print an error … but it doesn’t -_-

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:48:58+00:00Added an answer on May 27, 2026 at 4:48 am

    You said I just can't figure this out and honestly I can't get my head around what's going wrong. I think this is a great quote which sums up a large part of programming and software development in general — dealing with crazy, weird problems and spending a lot of time trying to wrap your head around them.

    Figuring out how to turn ridiculously complicated problems into small, manageable problems is the hardest part of programming, but also arguably the most important and valuable.


    Here’s some general advice which I think might help you:

    • use meaningful names for functions and variables (validateString doesn’t tell me anything about what the function does; string tells me nothing about the meaning of its contents)
    • break down problems into small, well-defined pieces
    • specify your data — what is a phone number? 10 positive digits, no spaces, no punctuation?
    • document/comment the input/output from functions if it’s not obvious

    Specific suggestions:

    • validateTel could probably be replaced with a simple regular expression match
    • try using json for serialization
    • if you’re using json, then it’s easy to use lists. I would strongly recommend this over using + as a separator — that looks highly questionable to me

    Example: using a regex

    import re
    
    def validateTel(call_ID):
        phoneNumberRegex = re.compile("^\d{10}$") # match a string of 10 digits
        return phoneNumberRegex.match(call_ID)
    

    Example: using json

    import json
    phoneNumber1, phoneNumber2, phoneNumber3 = ... whatever ...
    mylist = [phoneNumber1, phoneNumber2, phoneNumber3]
    print json.dumps(mylist)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering this code, can I be absolutely sure that the finally block always executes,
I'm not considering this - I'm comfortable with C# and VB, but an expert
I'm considering using this jquery utility as a possible solution, but I want to
considering this example: public static void main(final String[] args) { final List<String> myList =
Considering this xml document: DECLARE @X XML (DOCUMENT search.SearchParameters) = '<parameters xmlns=http://www.educations.com/Search/Parameters.xsd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance> <parameter
Considering this question of SO, where whole C# in-memory compiler is being called. When
I suppose this is a long shot considering how few Wonderware questions I've seen
I have this small code library that I'm considering releasing into Open Source. I
Like many others on this site I am considering a move to ASP.NET MVC
I apologize if this code looks a bit like a mess (considering the length);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.