简体   繁体   中英

Python while loop back to specific point in code

I'm very new to python. I need to loop back to a specific point in my code when a condition is not met.

Here is the code:

# Get user input on VLANs and interfaces to change
print ("what VLAN ID do you want to add? "),
vlan = raw_input()

print ("what interface do you want to add the VLAN to? (e.g. eth10)"),
interface = raw_input()

# Confirm details
print "So we are adding VLAN %r to interface %r" % (vlan, interface)

print ("Are the details above correct? (Yes/No)")
goodtogo = raw_input("> ")

if goodtogo == "yes":
    print "Configuring now...."

else:
    print "Please fix your error"

while goodtogo != "yes":
    print ("Starting again...")
    # Some type of code to loop back to start goes here!!

# Runs commands to add gathered config to switch
switch.runCmds(1, ["enable", "configure terminal", "vlan " + vlan, "interface " +     interface, "switchport mode access", "switchport access vlan " + vlan, "end" ])

print ("Change completed")

So what I need to happen is that when 'goodtogo' does not equal yes, to loop back to the start of the code. Really not sure what to do here...

def get_vlan_iface():
    while True:
        vlan = raw_input ("what VLAN ID do you want to add? "),
        iface = raw_input("what interface do you want to add the VLAN to? (e.g. eth10)")
        print "So we are adding VLAN %r to interface %r" % (vlan, interface)

        if raw_input("Are the details above correct? (Yes/No)>")[0].lower() == "y":
             return vlan,iface
        print "Please Fix Your Entries!"

vlan,iface = get_vlan_iface()

would be one way of doing it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM