1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Trying to learn python, stuck on a nested loop issue

Discussion in 'Programming & Webmastering' started by Seany1212, Mar 19, 2013.

  1. Seany1212

    Seany1212

    Joined:
    Jul 6, 2006
    Messages:
    670 (0.18/day)
    Thanks Received:
    28
    Location:
    England, UK
    Hey programming wizards,

    I'm trying to write a basic menu system after getting some inspiration off a few youtube videos. I'm very new to python and somewhat new to programming (know a little C#).

    def select():
    choice = int(input('Enter choice number: '))

    if (choice == 1):
    C2F()
    if (choice == 2):
    F2C()
    if (choice == 3):
    I2C()
    if (choice == 4):
    C2I()
    if (choice == 5):
    G2L()
    if (choice == 6):
    L2G()
    if (choice == 7):
    Y2M()
    if (choice == 8):
    M2Y()
    if (choice == 9):
    P2K()
    if (choice == 10):
    K2P()

    else:
    print ("Number", choice, "is an invalid selection");

    print ("Bye-bye.");

    What i'm trying to do is get the menu to loop with the message 'number x is an invalid selection' should they enter a number outside of the 1-10 range. If i remember from C# i used to be able to do a 'do while' loop but while i've looked at 'For' and 'While' loops here i'm :banghead: on figuring out how to implement them, maybe my if statements need to be reworked in order to get another type of nested loop going?

    Seany
     
    10 Year Member at TPU
  2. digibucc

    digibucc

    Joined:
    May 21, 2009
    Messages:
    4,966 (1.80/day)
    Thanks Received:
    1,519
    Seany1212 says thanks.
  3. Wrigleyvillain

    Wrigleyvillain PTFO or GTFO

    Joined:
    Oct 13, 2007
    Messages:
    7,704 (2.31/day)
    Thanks Received:
    1,791
    Location:
    Chicago
    Seany1212 and digibucc say thanks.
  4. Seany1212

    Seany1212

    Joined:
    Jul 6, 2006
    Messages:
    670 (0.18/day)
    Thanks Received:
    28
    Location:
    England, UK
    :respect:

    It took a bit for me to work out how i could implement into the code that i had, it turns out all i needed was:

    choice = int(input("\nPlease enter a number: "))
    while choice < 1 or choice > 10:
    choice = int(input("\nInvalid selection.\nPlease enter a number: "))
     
    digibucc says thanks.
    10 Year Member at TPU
  5. digibucc

    digibucc

    Joined:
    May 21, 2009
    Messages:
    4,966 (1.80/day)
    Thanks Received:
    1,519
    that's a lot simpler than i thought it'd be, very cool ;)
     
  6. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    9,714 (5.48/day)
    Thanks Received:
    4,794
    Location:
    Concord, NH
    You could also map the functions to a hash and call elements of the hash as if it were a function.

    Example:

    Code:
    #!/usr/bin/python
    
    def func_a():
        print "Function A runs."
    
    def func_b():
        print "Function B runs."
    
    def func_c():
        print "Function C runs."
    
    options = {
            0 : func_a,
            1 : func_b,
            2 : func_c,
    }
    
    
    arg1 = 0;
    arg2 = 1;
    arg3 = 2;
    
    options[arg1]()
    options[arg2]()
    options[arg3]()
    With output:
    Code:
    $ python test.py 
    Function A runs.
    Function B runs.
    Function C runs.
    I also recommend looking into command line arguments to provide input, such as which function should run.
    http://www.tutorialspoint.com/python/python_command_line_arguments.htm

    So you could do something like:
    Code:
    python test.py <function name>
    or
    Code:
    python test.py --function <function name>
     
  7. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (3.87/day)
    Thanks Received:
    5,632
    Location:
    Cheeseland (Wisconsin, USA)
    I suppose you could simplify that even more if you want ...
    Code:
    choice = -1
    while choice < 1 or choice > 10:
      choice = int(input("Please enter a number 1 though 10 : "))
    
    It won't tell them that an input is invalid, but if they can't figure out that entering 11 is not in the range 1 through 10 maybe they aren't smart enough to use the program anyway.
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)