当我使用正确的东西时,无法突破循环

时间:2018-03-27 02:11:54

标签: python

def G(): 
    def gl(): 
        url = ("www.google.com");
        webbrowser.open_new(url);   ##takes me to google
        exit();

    def glin(): 
        os.startfile("C:\Program Files\M Google\Google Chrome.lnk");    ##takes me to google incognito
        exit();

    while 1: 
        what_to_doG = input("choosing which one of glin or gl");  ##chooses which one of the above to go to
        if ( what_to_doG == gl): 
            gl();
            break;


        if ( what_to_doG == glin): 
            glin();
            break;


while 1: 
    what_to_do = input("choosing out of G or B");

    if ( what_to_do == G): 
        G();                           ##chooses which one to go to
        break;
  ##   ^^^^^^--- this won't take me to G() and won't break out

    if (  what_to_do == B): 
        B();
        break;

当我运行它时,它会一直回到“选择G或B”,即使我正在使用G,请帮我一个人,我这样做可以更快地找到标签

2 个答案:

答案 0 :(得分:1)

试试这个。您需要将字符串与字符串进行比较,如what_to_do == 'G'

def G(): 
    def gl(): 
        url = ("www.google.com")
        webbrowser.open_new(url)
        exit()

    def glin(): 
        os.startfile("C:\Program Files\M Google\Google Chrome.lnk")   
        exit()

    while 1: 
        what_to_doG = input("choosing which one of glin or gl");  
        if what_to_doG == 'gl': gl()
        elif what_to_doG == 'glin': glin()
        break


while 1: 
    what_to_do = input("choosing out of G or B")
    if what_to_do == 'G': G()
    elif what_to_do == 'B': B()
    break

答案 1 :(得分:0)

我运行代码时注意到的是你需要在G和B周围加上引号:

if ( what_to_do == 'G'): 
    G();                           ##chooses which one to go to
    break;

if (  what_to_do == 'B'): 
    B();
    break;

之后它对我有用。希望这可以帮助! 修改 你还需要在glin和gl

周围加上引号