[Tutor] Console application

Alan Gauld alan.gauld at btinternet.com
Sat Jan 16 19:17:17 EST 2016


On 16/01/16 21:18, Ali Moradi wrote:
> Hi, i don't have any clue how to write a console program that shows a list
> of options which could be chosen with keyboard and when one item was
> selected, a text shows there.
> 
> I want to know, which things i need to write a program like that?
> 
> Input() , print, and .... What? :(


There are lots of ways to do this. The simplest is simply to
display a menu and ask the user to select an entry.

print('''
1 New
2 Edit
3 Delete
4 quit
''')
cmd = int(input('Enter an option number')

if cmd == 1:....
elif cmd == 2:....
etc

If the above explanation is still too complicated
then please come back with more questions

You can add loops and error traps if you wish.
You can put the menu inside a function. But I don't
know how much of that extra stuff you understand yet.

Another, more advanced/professional, option, which builds
applications that look like Python's help() function, is
to use the cmd module. There are examples in the documentation

Finally, if you are on *nix or MacOS you can use curses
to build a pseudo-GUI that works in a terminal. But
that is almost certainly too much for you at this stage.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list