Click here to Skip to main content
15,559,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello this is my first project to learn python
i made phonebook but i feel the coding is not good its too long for small project
how can i make it smaller ?

when you started it ask you what you like to do add new phone and name or search by name or phone

What I have tried:

Python
# data
names = ['salman','Amal','sarah','malik','lamar','mohammed']
numbrs = [11,22,33,44,55,66]

# input number
def enter_numbr():
    while True:
        try:
            num = int(input('enter phone number:'))
        except ValueError:
            print('sorry enter only numbers')
            continue
        else:
            break
    # search in data
    try:
        va = numbrs.index(num)
        print(names[va], numbrs[va])
    except ValueError:
        print('the number you entered is not saved')

# input name
def enter_name():
    while True:
        try:
            name = (input('enter name:'))
        except ValueError:
            print('sorry enter only name')
            continue
        else:
            break
    # search in data
    try:
        va1 = names.index(name)
        print(names[va1], numbrs[va1])
    except ValueError:
        print('the name you entered is not found')

# save new number
def save_number():
    print('save new number')
    new_name = input('add the name:')
    names.append(new_name)
    new_number = int(input('add the number:'))
    numbrs.append(new_number)
    print('the nunber that you saved is:','\n',new_name,new_number)

# chose options
def chose():
    while True:
        ch1 = input('chose new or look:')
        if ch1 == 'look':
            while True:
                ch2 = input('do you like to look by name or number\n type "name" or "number":')
                if ch2 == 'name':
                    enter_name()
                    break
                elif ch2 == 'number':
                    enter_numbr()
                    break
                else:
                    print('enter only "name" or "number"')
        elif ch1 == 'new':
            save_number()
            break
        else:
            print('type only "new" or "look"')

while True:
    chose()
Posted
Updated 5-Mar-22 4:20am

1 solution

The question you should probably be asking isn't "how big is it?" - it's "how good is it?".

Why do you think that's a "big project"? If it's a complete phone book app (and it isn't, the names and numbers would need to be persistent for that) than it's pretty small. In fact, it's a pretty small project all round - "real world" projects will rarely be anywhere near that tiny!

Writing code isn't a competition to see how small you can make it, it's about making it reliable, understandable, and maintainable - and your code is simple enough that it fills the last two criteria at least, so probably should be reworked just to shrink the number of lines.

From a user perspective? It's pretty dumb to use: anyone who needed a phone book app would problem grow to hate using it pretty soon as it involves way too much accurate typing! :D

But for a beginner, it's a good fist at the genre. Well done - now make newly entered data persistent!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900