• 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

Joined
Jul 6, 2006
Messages
666 (0.10/day)
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
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 (0.92/day)
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

Wrigleyvillain

PTFO or GTFO
Joined
Oct 13, 2007
Messages
7,702 (1.28/day)
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
Joined
Jul 6, 2006
Messages
666 (0.10/day)
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
Joined
May 21, 2009
Messages
4,966 (0.92/day)
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
that's a lot simpler than i thought it'd be, very cool ;)
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,147 (2.96/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
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
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
: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.
 
Top