我有这段代码:
p1 = "Hey"
p2 = "Hey2"
p3 = "Hey3"
p4 = "Hey4"
p5 = "Hey5"
def ifdisplaygrid(x1,x2,x3):
x1 += "Y"
x2 += "N"
x3 += "K"
if x1 == p3 and x2 == p4 and x3 == p5:
print p1 + p2 + x1 + x2 + x3
else:
print "Function Is Not Working!!!"
ifdisplaygrid(p3,p4,p5)
我希望输出为:
HeyHey2Hey3YHey4NHey5K
但这种情况从未发生过,我在这里做错了什么?谢谢!
答案 0 :(得分:3)
函数的参数按值类型复制,因此当您更改x1-x3时,原始变量不会更改。因此if语句是错误的。
答案 1 :(得分:1)
在if条件检查之前,你已经修改了x1,x2和x3的值,因此它永远不会等于p3,p4和p5。在if条件中添加该连接逻辑将使您的代码正常工作
def ifdisplaygrid(x1,x2,x3):
if x1 == p3 and x2 == p4 and x3 == p5:
x1 += "Y"
x2 += "N"
x3 += "K"
print p1 + p2 + x1 + x2 + x3
else:
print "Function Is Not Working!!!"