Hello i'm rather new to Python and I was wondering how I could do a menu to prompt the user to choose a number and execute a certain part of code based on his choice.

For example:

please choose an option
1)
2)
3)

then based on users input, it executes only that part of the code.

Would i just do something like

if choice ==1 :
do this

if choice ==2:
do this

ect?

Recommended Answers

All 2 Replies

Would i just do something like

if choice ==1 :
do this

if choice ==2:
do this

ect?

Yeah pretty much. That's the most straight forward way to approach this problem

usr_inp = raw_input( 'Enter your choice (1-3): ' )

if usr_inp == '1':
    # do something for 1
elif usr_inp == '2':
    # do another thing for 2
elif usr_inp == '3':
    # do the number 3 thing
else:
    print 'Invalid Option'

I posted a code snippet for this kind of problems. It's an elementary solution, but it works. A more sophisticated approach is to use the standard module cmd

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.