If statement python条件问题(2.7)

If statement python条件问题(2.7),if-statement,python-2.7,If Statement,Python 2.7,cccI有一个让我抓狂的python脚本。这是违规代码。。。if语句中的代码未运行,有人能提出原因吗 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("co

cccI有一个让我抓狂的python脚本。这是违规代码。。。if语句中的代码未运行,有人能提出原因吗

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'

您的
refresh\u type
值不是
update
,而是
'update'
。其他变量也有同样的问题,它们在字符串值中有引号

使用
print“refresh\u type is:”,repr(refresh\u type)
作为诊断帮助

您可以使用:

if refresh_type == "'update'":
但是你有一个更基本的问题,就是在字符串值中有额外的引号

举例来说,一个简短的Python解释器会话:

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

您的
refresh\u type
值不是
update
,而是
'update'
。其他变量也有同样的问题,它们在字符串值中有引号

使用
print“refresh\u type is:”,repr(refresh\u type)
作为诊断帮助

您可以使用:

if refresh_type == "'update'":
但是你有一个更基本的问题,就是在字符串值中有额外的引号

举例来说,一个简短的Python解释器会话:

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

你可以更容易地使用括号<代码>打印不是python 2.7中的函数。你说的“if语句没有运行”是什么意思?到目前为止,我对python的一个抱怨是缺少描述动作的东西。我喜欢把括号放在那里,因为它能让我马上明白我在做什么。是的,你不需要它们,但这对我来说就像在它周围画一个盒子。我喜欢。我会继续做下去。在其他语言中,像分号这样的行尾字符和函数大括号都没有问题。我也喜欢你可以更容易地使用括号<代码>打印不是python 2.7中的函数。你说的“if语句没有运行”是什么意思?到目前为止,我对python的一个抱怨是缺少描述动作的东西。我喜欢把括号放在那里,因为它能让我马上明白我在做什么。是的,你不需要它们,但这对我来说就像在它周围画一个盒子。我喜欢。我会继续做下去。在其他语言中,像分号这样的行尾字符和函数大括号都没有问题。我也喜欢repr(刷新类型)是一个很好的提示。我是从一个配置文件中得到这个值的,该文件的refresh\u类型带有引号。谢谢。repr(刷新类型)是一个很好的提示。我是从一个配置文件中得到这个值的,该文件的refresh\u类型带有引号。谢谢