• We've upgraded our forums. Please post any issues/requests in this thread.

Trying to learn python, stuck on a nested loop issue

Joined
Jul 6, 2006
Messages
669 (0.16/day)
Likes
28
Location
England, UK
Processor AMD Athlon 3800+ X2
Motherboard ASUS A8N-SLI SE
Cooling 2 Exhaust fans
Memory 2x512MB DDR400 RAM
Video Card(s) Connect3D ATI Radeon X1900XT 512MB
Storage 80GB Maxtor DiamondMax
Display(s) Packard bell 17" CRT
Case Stock
Power Supply 580W Hiper Type-R
#1
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
 
Joined
May 21, 2009
Messages
4,966 (1.59/day)
Likes
1,511
System Name i7-PC / HTPC / iMac
Processor i7 3820 / Phenom II 940
Motherboard GIGABYTE G1.ASSASSIN2 / M3A79-T Deluxe
Cooling Corsair Hydro H100i / Scythe II (HS only)
Memory G.SKILL Trident X Series 8GB (2 x 4GB) DDR3 1600mhz / 4GB DDR2 1066 (@800) Corsair Dominator
Video Card(s) GB Radeon HD 7950s 3GB / GB Radeon HD 7950s 3GB
Storage 2x 80GB Intel X-25, 2x600gb SATA, 1x1tb 5400RPM storage /1x600GB, 3x500GB,1x160,1x120 SATA
Display(s) 1x 27" Yamakasi / Vizio 42" HDTV
Case Lian Li Lancool PC-K58 / Antec 900
Audio Device(s) HT Omega Striker 7.1 / Onboard and HDMI from ATi Card
Power Supply PC Power & Cooling 750W / 610W
Software Ubuntu / Windows 8.1 Pro / OS X / PHPStorm / Gaming
#2
Joined
Oct 13, 2007
Messages
7,693 (2.07/day)
Likes
1,772
Location
Chicago
System Name DarkStar
Processor i5 3570K 4.4Ghz
Motherboard Asrock Z77 Extreme 3
Cooling Apogee HD White/XSPC Razer blocks
Memory 8GB Samsung Green 1600
Video Card(s) 2 x GTX 670 4GB
Storage 2 x 120GB Samsung 830
Display(s) 27" QNIX
Case Enthoo Pro
Power Supply Seasonic Platinum 760
Mouse Steelseries Sensei
Keyboard Ducky Pro MX Black
Software Windows 8.1 x64
#3
Joined
Jul 6, 2006
Messages
669 (0.16/day)
Likes
28
Location
England, UK
Processor AMD Athlon 3800+ X2
Motherboard ASUS A8N-SLI SE
Cooling 2 Exhaust fans
Memory 2x512MB DDR400 RAM
Video Card(s) Connect3D ATI Radeon X1900XT 512MB
Storage 80GB Maxtor DiamondMax
Display(s) Packard bell 17" CRT
Case Stock
Power Supply 580W Hiper Type-R
#4
Joined
May 21, 2009
Messages
4,966 (1.59/day)
Likes
1,511
System Name i7-PC / HTPC / iMac
Processor i7 3820 / Phenom II 940
Motherboard GIGABYTE G1.ASSASSIN2 / M3A79-T Deluxe
Cooling Corsair Hydro H100i / Scythe II (HS only)
Memory G.SKILL Trident X Series 8GB (2 x 4GB) DDR3 1600mhz / 4GB DDR2 1066 (@800) Corsair Dominator
Video Card(s) GB Radeon HD 7950s 3GB / GB Radeon HD 7950s 3GB
Storage 2x 80GB Intel X-25, 2x600gb SATA, 1x1tb 5400RPM storage /1x600GB, 3x500GB,1x160,1x120 SATA
Display(s) 1x 27" Yamakasi / Vizio 42" HDTV
Case Lian Li Lancool PC-K58 / Antec 900
Audio Device(s) HT Omega Striker 7.1 / Onboard and HDMI from ATi Card
Power Supply PC Power & Cooling 750W / 610W
Software Ubuntu / Windows 8.1 Pro / OS X / PHPStorm / Gaming
#5
that's a lot simpler than i thought it'd be, very cool ;)
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
10,397 (4.85/day)
Likes
5,476
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#6
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>
 

Kreij

Senior Monkey Moderator
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.49/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#7
: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: "))
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.