Python Forum
user friendly making a list and changing it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
user friendly making a list and changing it
#1
Hi,
I'm learning python ver 3.(x) programming.

I have been developing a program to learn alot about list manipulation, so I'm currently working on a "phonelist"program, I have it working with all the features of inputing names and numbers, appending and saving to files, and doing search's by name or number, etc..
however one feature I havn't yet incorporated into it is the ability for the user to change the data in the list, to update the list with a changed name or number.
So I set out to work on subprograms, to learn the algorythm of accomplishing this feature, and I came up with a feature that works with limited ability, but it works good enough to be a standalone program in itself.
So I would like to share this subprogram with other beginners, for them to have fun learning with it and hacking it to make it work for there use.

As I said I'm only a beginner myself, so I know the code is not to professional standards, but I am only a hobbyist in programming, and I like to share the things I'm learninjg at the beginners level.

Thanks for understanding.

Here is the program:
import os
class klass:
     '"docstring'"
    def_init_(self):
        self.attribute="var"
        if len(self.attribute) <2:
            pass

print("")
print ("Enter a subject you would like to build up a list for!")
print("Ex. (name,city,state,number,team,job,friend,hobby...etc...)")
print("In other words you'r making a (name list, hobby list or a job list etc...)")
print("")
userstart=input("subject to make a list for>>  ")
lista=[]
flg=0
add=0
while flg==0:
    print ("Enter (exit) to exit")
    print("")
    user = input ("enter a, "+(userstart)+" >>  ")
    print("")
    if user=="exit":
        flg=1
    else:
        lista.append(user)
        add=1
chng=0
if add==1:
    print ("Here is your ,"+(userstart)+" list, to date, ")
    print("")
    print("       ROW          ITEM")
    print("---------------------------")
    cnt=0
    for item in lista:
        cnt+=1
        print ("      (",(cnt),")        ",(item))
    chk=0
    ask=0
    while ask==0:
        print("")
        print("Would you like to change an item in your, "+(userstart)+" list?")
        user=input("change and item?  (Y) or (N)  ")
        if user =="y":
            print ("")
            while chk==0:
                user=input("enter the row number to change that item  ")
                try:
                    user=int(user)
                    row=0
                    for item in lista:
                        chk=1
                        row+=1
                        ndx=lista.index(item)
                        if row==(user):
                            newndx=ndx
                            chng=1
                            ask=1
                except:
                    print("")
                    print ("Enter a row number from the list")
            if chk==1 and chng==0:
                print("")
                print("Choose a row number from the list")
                chk=0
        elif user=="n":
            ask=1
        else:
            print("")
            print("enter (y) or (n)")
    change=0
    while change==0:
        if chng==1:        
            print("")
            user=input("Enter a new item to take the place>>  ")
            print("")
            lista[newndx]=(user)
            cnt=0
            for item in lista:
                cnt+=1
                print ("(",(cnt),")      ",(item))
                print("")
            print ("The change has been made thankyou")
            change=1
        else:
            change=1
       
print("")
print("Done")
Reply
#2
If you want to explore the console interaction with the user, you can use the builtin cmd module to create interactive programs. For example
you can have a session such as
Output:
Welcome to Interlist. Type help or ? to list commands. [Interlist] subject color Subject set to color [Interlist] add yellow [Interlist] add blue [Interlist] add green [Interlist] list 1 yellow 2 blue 3 green [Interlist] update 2 red [Interlist] [Interlist] list 1 yellow 2 red 3 green [Interlist] update 5 black error: index 5 is too high [Interlist] update 4 black [Interlist] list 1 yellow 2 red 3 green 4 black [Interlist] update 0 white error: index 0 is too low [Interlist] update 3 white [Interlist] list 1 yellow 2 red 3 white 4 black [Interlist] subject The current subject is color [Interlist] exit Thank you for using Interlist.
Here is the program that allows this
import cmd, sys

class InterlistShell(cmd.Cmd):
    intro = 'Welcome to Interlist.   Type help or ? to list commands.\n'
    prompt = '[Interlist] '
    
    def __init__(self):
        super().__init__()
        self.subject = '<undefined>'
        self.userlist = []
    
    def emptyline(self):
        pass

    def do_subject(self, arg):
        args = arg.strip().split()
        if not args:
            print("The current subject is", self.subject)
        elif (len(args) == 1) and args[0]:
            self.subject = args[0]
            print("Subject set to", self.subject)
        else:
            print("usage: subject word", args)
    
    def do_list(self, arg):
        if arg.strip():
            print("usage: list")
            return
        for i, item in enumerate(self.userlist, 1):
            print('{: <3d} {}'.format(i, item))

    def do_add(self, arg):
        item = arg.strip()
        if not item:
            print("usage: add ITEM")
            return
        self.userlist.append(item)

    def do_update(self, arg):
        s = arg.strip()
        i = s.find(' ')
        if i == -1:
            print("usage: update INDEX VALUE")
            return
        try:
            index = int(s[:i])
        except ValueError:
            print("usage: update INDEX VALUE")
            return
        value = s[i+1:].lstrip()
        if not value:
            print("usage: update INDEX VALUE")
            return
        if index-1 < 0:
            print("error: index {} is too low".format(index))
        elif index-1 < len(self.userlist):
            self.userlist[index-1] = value
        elif index-1 == len(self.userlist):
            self.userlist.append(value)
        else:
            print("error: index {} is too high".format(index))

    def do_exit(self, arg):
        'Quit the program'
        print('Thank you for using Interlist.')
        return True # this tells the cmdloop to exit
    do_quit = do_exit

if __name__ == '__main__':
    InterlistShell().cmdloop()
Reply
#3
Hi,
Thankyou for taking the time to post that code, very much appreciated.

I tried to read it, with gaining some understanding of how the flow control
was written, and realized, wow, that's waaay beyond me for right now.
I look at my program, and then your's, and can see the difference between, elementary level
versus professional level. (of course it goes without saying mine is the elementary level).

Thanks again for the comment.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Avoiding new user list sorting errors ljmetzger 1 2,758 Apr-07-2018, 04:03 PM
Last Post: Larz60+
  user defined list-based calculator mepyyeti 1 2,665 Mar-31-2018, 10:25 PM
Last Post: Almenon

Forum Jump:

User Panel Messages

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