mercredi 18 décembre 2019

What is wrong with the condition in the if loop here? [duplicate]

This question already has an answer here:

So here is the Python 3 code I am trying to run:-

z1 = "-0530"
z2 = "+0130"
h1 = int(z1[1:3])
m1 = int(z1[3:])
h2 = int(z2[1:3])
m2 = int(z2[3:])

if z1[0] and z2[0] == "+":
    print(1)
    diff = ((h1 - h2) * 3600) + (m1 - m2) * 60)
elif z1[0] == "+" and z2[0] == "-":
    print(2)
    diff = ((h1 + h2) * 3600) + ((m1 + m2) * 60)
elif z1[0] == "-" and z2[0] == "+":
    print(3)
    diff = ((h1 + h2) * 3600) + ((m1 + m2) * 60)

print(diff)

So by looking at the values of z1 and z2 you can see that the second elif loop, the one containing print(3) should run. But as I was not getting the correct output, I put in those

print(1)
print(2)
print(3)

statements to see which loop ran and turned out the program was running the main if loop containing print(1).

How is this happening? What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire