hey python programmers... I do not know how to make a choice input. like so that I can ask lik choice=raw_input('what do you want to do') and have a few lettters serve as the choices... i know that i can do a weird work around with if: and else: but there MUST be a better way... im a noob
use switch with cases
? i got no clu
and thanks for checking my question... i just started in python class. i know some qBASIC and py looks and works similar enough to get it sort of so im ahead of the class in this but i am doing a side project to learn much faster
lol well switch is a multiple decision statement, look this: http://www.mustap.com/pythonzone_post_224_python-switch-statement
ok
looking now
now will that play nice with my raw input or input ... im looking at a few examples and some code and it almost makes sense to me
I'm new to python as well, (I've only written 2 programs in py, Conway's game of life and an automated testing framework.) but fr33's answer is appropriate for many languages. Initalize a choice, set to null or 0; Scanner choice = next keboard input; while choice != exit condition: Print list of options to the viewer switch(choice) case 1 run 1st option method case 2 run 2nd option method blah blah case 5 option = exit condition
ok ... im still just not clear as to what goes where and i dont understand... i can get it if you explain i just am looking at code tho
Can you be a little more specific with your question? Do you have an attempt you would like me to look at?
and how do i initialize a choice
if '0516335' in user: print 'Hello Daniel!' print ' ' passw=raw_input('Password: ') if '8040379129' in passw: print 'Processing', import time time.sleep(1) print '.', time.sleep(1) print '.', time.sleep(1) print '.' dowht=raw input dowht (var) #here i want to have a choice to append or just open a doc or just something to that effect { case value1: do_some_stuff1(); case value2: do_some_stuff2(); ... case valueN: do_some_stuffN(); default: do_default_stuff(); } import subprocess # #actual hidden file # filep='C:\Daniel\Folders\Maze\L1 11\L2 2\L3 17\mvbj.doc' wrdp='C:\Program Files\Microsoft Office\Office12\WINWORD.exe' subprocess.Popen([wrdp,filep])
just plugged in the case thing that fr33 told me to look up
Sorry. Initialization and declaration are really loose in Python, I forgot about that. Yeah. What you've got seems pretty close, minus raw input and a few other instances. Have you tried cleaning it up and running it?
ya it runs perfect but this is v1.1 lol if you can call it that...
We all start somewhere. A running program is worthy of a beer where I come from no matter how simple.
its all running nice together BUT i wanted to make it more versitile... right now it only password protects and finds my files and opens them in word
So your question isn't really about option lists? It's about new functionality?
well yea iv dun all my hello world stuff already
well im trying to give myself options lik appending an entry from the shell to the document saving it from there and things lik that
You want your python program to just feed user input into the shell?
into the document it is opening but i want to be able to make choices lik if i say 's' it saves the file or 'a' for append or o for open
Won't the program you have do exactly that? You just have to fill in the methods where you have to choices.
Hang on, you want to program to act as like a word application that prints to a predetermined document, and accepts certain keystrokes as special commands such as save, etc.?
ya .... but not so i have a word editor.... im trying to learn these concepts and i have a feeling thats a little far off in my lessons
I've got to bail. If what I just asked is the case, I would do it something like this. def wordprog() option2 = null; while option2 != "X" print to file main wordprog() present the optionlist you already have in place to the user (i.e. save, etc., just make one of the options wordprog() and present that to the user as "Continue what you were doing."
This way anytime the user types just a capital X and hits enter, he gets taken to the second menu with the options in place; and he can exit for good that way, otherwise when he runs the program he just goes straight into typing.
this may just be something i wont understand either way for a bit cuz none of this is making sense to me... thanks for your help though
If you want to pythonic way of solving the problem look here http://paste.pocoo.org/show/431321/
Join our real-time social learning platform and learn together with your friends!