Python循环回到代码中的特定点

时间:2014-02-26 22:54:57

标签: python

我对python很新。当条件不满足时,我需要循环回代码中的特定点。

以下是代码:

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

所以我需要发生的是当'goodtogo'不等于是时,循环回到代码的开头。真的不知道该怎么做......

1 个答案:

答案 0 :(得分:1)

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

将是这样做的一种方式