# Trying to learn python, stuck on a nested loop issue



## Seany1212 (Mar 19, 2013)

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  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


----------



## digibucc (Mar 19, 2013)

check this out, it's a similar question. I know php not python but this should help you.

http://stackoverflow.com/questions/8042609/python-while-loop-range


----------



## Wrigleyvillain (Mar 19, 2013)

Starts in three weeks:

https://www.coursera.org/course/interactivepython

I am enrolled but just finishing up an Intro to Networking class that kind of kicked my ass so may wait on this until offered again.


----------



## Seany1212 (Mar 20, 2013)

digibucc said:


> check this out, it's a similar question. I know php not python but this should help you.
> 
> http://stackoverflow.com/questions/8042609/python-while-loop-range





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 (Mar 20, 2013)

that's a lot simpler than i thought it'd be, very cool


----------



## Aquinus (Mar 20, 2013)

You could also map the functions to a hash and call elements of the hash as if it were a function.

Example:


```
#!/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:

```
$ 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:

```
python test.py <function name>
```
or

```
python test.py --function <function name>
```


----------



## Kreij (Mar 20, 2013)

Seany1212 said:


> 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: "))



I suppose you could simplify that even more if you want ...

```
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.


----------

