简体   繁体   中英

Error with if statement and multiple conditions

I'm having a problem with if statement that's checking if input is correct. It when I input M or F it prints out "Wrong input" and I don't understand it very clearly.

def check(aw,iw):
    if abs(aw-iw)<5 :
        print "Your weight is normal."
    elif abs(aw-iw)>5 and abs(aw-iw)<15 :
        print "Your weight is about normal."
    else:
        print "Your weight is not normal."
    return

print "Enter your gender (M/F)"
gender=raw_input()
if gender!='M' or gender!='F':
    print "Wrong input."
else:
    if gender=='M':
        w=raw_input("Enter your weight (kg) :")
        h=raw_input("Enter your height (cm) :")
        idw=110-h
        check(w,idw)
    else:
        w = raw_input("Enter your weight (kg) :")
        h = raw_input("Enter your height (cm) :")
        idw = 110 - h
        check(w, idw)

Every input is either not equal to M or not equal to F (eg, M is not equal to F ). Instead you need to check if your input is not equal to M and not equal to F :

if gender != 'M' and gender != 'F':
    # Here ------^
    print "Wrong input."

Or, more elegantly, use the not in operator:

if gender not in ('M', 'F'):
    print "Wrong input."

This line is incorrect:

if gender!='M' or gender!='F':

It will always resolve to False since gender can never be both M and F .

You can use in instead:

if gender in ('M', 'F'):

Alternatively:

if (gender != 'M') and (gender != 'F'):

Also, remove the line gender=int(gender) : it should always fail.

I hope you have already got your answer in the previous answers to your question. i would just like to add the fact you appreciate some visualizations to the simple problem that you have faced but very delicate one for every beginner.

You have or doing this- when you write

M

python checks if

M is != (not equal to) M

but M is equal to M. so it results in false and then checks if

M != F

and sees its true. The or operation thus does the following-

  • true + true = true
  • true + false= true
  • false + true = true ....... this one
  • false + false = false

As your one resembles the 3rd one it returns true and python says "wrong input".

if you write and then it goes something like- is

M!=M

returns false, and

M!=F

returns true.

In and its like-

  • true + true= true
  • true + false= false
  • false + true= false
  • false + false= false

So python returns false for the if statement and follows what you want it to do in the next lines of code. I hope it makes it very clear.

And remove gender=int(gender) its not necessary because python can recognize M and F as char and you need not turn it into integer value or ascii here.

Remove the line

gender=int(gender)

and make changes to

 if gender!='M' or gender!='F':

as

if gender!='M' and gender!='F':

Also, you should use <= to include limits in your evaluation.

def check(aw,iw):
    if abs(aw-iw)<=5 :
        print "Your weight is normal."

You can shorten elif abs(aw-iw)>5 and abs(aw-iw)<15: , since if abs(aw-iw)<5 is not true, then it will always be higher than 5.

    #Instead of elif abs(aw-iw)>5 and abs(aw-iw)<15:
    elif abs(aw-iw)<=15:
        print "Your weight is about normal."
    else:
        print "Your weight is not normal."
    return

Instead of:

print "Enter your gender (M/F)"
gender=raw_input()
gender=int(gender)

You should use:

gender = raw_input('Enter yor gender (M/F)')

Where int(gender) would cause an error, since the input you want from gender=raw_input() gives a string in letters.

You should change that or for an and , since if using or , if your input is 'M' , then gender != 'F' will give True and it will run the if condition.

#Instead of if gender!='M' or gender!='F':
if gender!='M' and gender!='F':
    print "Wrong input."

Here you should use int(raw_input()) for your variables to be evaluated as integers and be able to add or subtract them. And since you are doing the same code wether it's M or F , there is no need to write another if: else: condition.

else:
    w = int(raw_input("Enter your weight (kg) :"))
    h = int(raw_input("Enter your height (cm) :"))
    idw = 110 - h
    check(w, idw)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM