if-statement
  • python-2.7
  • 2013-01-01 36 views 1 likes 
    1

    cccI有一個python腳本,這讓我瘋狂。這是有問題的代碼... if語句代碼沒有運行,有人可以說明爲什麼?:python有條件的問題(2.7)

    print("refresh_type is: " + refresh_type) 
    
    if (refresh_type == "update"): 
        print("hello world") 
        cmd = 'sudo svn %s --username %s --password %s %s' % ('up', un, pw, working_path) 
        print("command is: " + str(cmd)) 
    elif(refresh_type == 'checkout' or refresh_type == 'co'): 
        cmd = 'sudo svn %s --username %s --password %s %s %s' % (refreshType, un, pw, rc_service_url, working_path) 
    
    print('username = ' + credentials['un']) 
    print('password = ' + credentials['pw']) 
    print("working path = " + working_path) 
    

    這裏是打印報表的輸出:

    refresh_type is: 'update' 
    username = 'cccc' 
    password = 'vvvvv' 
    working path = '/home/ubuntu/workingcopy/rc_service' 
    
    +0

    你可以在括號更容易出現了很多;在python 2.7中'print'不是*函數。你的意思是'if語句沒有運行'? –

    +0

    到目前爲止,我對python的一個抱怨是缺乏劃分行爲的東西。我喜歡把括號放在那裏,因爲它使我立刻明白我在做什麼。是的,你不需要他們,但這對我來說就像在周圍畫一個盒子。我喜歡。我會繼續這樣做。我不會遇到像其他語言中的分號一樣的結尾字符,也不會使用大括號來表示函數。我也喜歡他們。 :) – BillR

    回答

    4

    refresh_type值是不是update,它是'update'。其餘變量患有相同的疾病,他們報價爲,作爲字符串值的一部分。

    使用print "refresh_type is:", repr(refresh_type)作爲診斷的輔助手段。

    你可以使用:

    if refresh_type == "'update'": 
    

    但你有一個更根本的問題,一個你在哪裏結束在你的字符串值額外的引號。

    爲了說明,一個簡短的Python解釋器會話:

    >>> print 'update' 
    update 
    >>> print "'update'" 
    'update' 
    >>> "'update'" == 'update' 
    False 
    >>> foo = "'update'" 
    >>> print repr(foo) 
    "'update'" 
    
    +0

    repr(refresh_type)是一個很好的提示。我從具有帶引號的refresh_type的配置文件中獲取此值。謝謝。 – BillR

    相關問題