Hi there, I am interested in phone selection by using if..else statement. so after viewing the phone specification we can select the phone and purchase that. so do you have python sample codes for that? a rough idea is enough. Thank you. and python is really interesting.

3 Years
Discussion Span
Last Post by Schol-R-LEA

It's not entirely clear what you want, and I doubt anyone would have an example quite like that on hand in any case. It sounds as if you want to know who to provide a menu, is that more or less correct? If so, a general model for what you want would be something like this:

menu = '''Please choose from the following options:
   1. option 1
   2. option 2
   3. option 3
   4. option 4'''

option = input(menu)

while option not in ['1', '2', '3', '4']:
    print('sorry, that is not a valid option, please choose again.\n')
    option = input(menu)

if option == '1':
    # perform the first option
elsif option == '2':
    # perform the second option
elsif option == '3':
    # perform the third option
    # perform the fourth option

This is just a general sketch of a menu; you should be able to fill in the details yourself.If the number of options is very large, you might want to do this instead:

def option_one():
   # do option one code

def option_two():
   # do option two code

def option_three():
   # do option three code

def option_four():
   # do option four code

# and so on...

if __name__ == '__main__':
    ## do the part that read in the option as before...

    # make a dictionary mapping options to functions
    option_list = {
        '1': option_one, '2': option_two, '3': option_three, 
        '4': option_four, '5':option_five, 
        # and so on... 
        'n': option_n

    for choice in option_list:
        if choice == option:

thank a lot. This is helping me. If got any questions definetly will back to you. Thanks . I appreciates it well.


sigh I must have been tired, or else had an attack of the dumba-- this morning. You can replace

    for choice in option_list:
        if choice == option:

with just


There's no need to loop on it at all. My bad.

Edited by Schol-R-LEA

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.