Nasif Imtiaz Ohi
Nasif Imtiaz Ohi

Reputation: 1713

getting a nameerror in python. while two other name is working, one name is not working..!

I'm a beginner in Python; if I write this code it shows:

 Traceback (most recent call last):
  File "C:/Python32/ultimate tym remaining.py", line 37, in <module>
    print("so the reamining time is %d hour, %d minutes and %d seconds" %(hr,minr,sr))
 NameError: name 'minr' is not defined 

My code was:

hg=12
mg=00
sg=00
hn=10
mn=47
sn=49

if(mg>mn & hg>hn):
    hr=hn+24-hn-1
elif(mg<mn & hg>hn):
    hr=hn+24-hg
elif(mg>mn & hg<hn):
    hr=hn-hg-1
elif(mg<mn & hg<hn):
    hr=hn-hg
elif(hg==hn):
    hr=0

if(sg<sn & mg>mn):
    minr=mn+60-mg
elif(sg<sn & mg<mn):
    minr= mn-mg
elif(sg>sn & mg>mn):
    minr=mn+60-mg-1
elif(sg>sn & mg<mn):
    minr=mn-mg-1
elif(mg==mn):
    minr=0

if(sg>sn):
    sr=sn+60-sg
elif(sg<sn):
    sr=sn-sg
elif(sf==ss):
    sr=0

print("so the reamining time is %d hour, %d minutes and %d seconds" %(hr,minr,sr))

Why is this happening? Why are hr and sr working and minr is not?

Upvotes: 0

Views: 375

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601659

The following if construct is defining minr:

if(sg<sn & mg>mn):
    minr=mn+60-mg
elif(sg<sn & mg<mn):
    minr= mn-mg
elif(sg>sn & mg>mn):
    minr=mn+60-mg-1
elif(sg>sn & mg<mn):
    minr=mn-mg-1
elif(mg==mn):
    minr=0

What's happening is simple: None of the conditions happens to be True, leaving minr undefined. This can even happen after fixing the logical AND operator – it's and, not &, as pointed out by Amber. sg == sn might hold, while mg != mn.

Upvotes: 7

Related Questions