1

I am trying to get a code using if else condition.I wish to take values from if else condition .

Currently if condition works, not working for else condition.

mname=input("Enter  name: ")
    m=[]

        if mname=="CS1TFD22" or "cs1tfd22":
            mcode='CS122S003'
            if l1l2=="NULL":
                icode = 'CS122S003d13_mh_'
            elif l1l2!="NULL":
                icode = 'CS122S003d13_L1_mh_'

        else:
            for i in mname:
                m.append(i)

            mcode = 'CS1'+m[5]+m[6]+'S003'
            if l1l2=="NULL":
                icode='CS1'+m[5]+m[6]+'S003d113_mh_'
            elif l1l2 != "NULL":
                icode = CS1'+m[5]+m[6]+'S003d13_L1_mh_'

        print(mcode,icode)

Output I get is always mcode='CS122S003' and icode='CS122S003d13_L1_mh_', if mname is not 'CS1TFD22'. For example if I enter mname as CS1TFD23 , then icode should be 'CS123S003' and icode should be 'CS123S003d13_mh'

How to work for else condition also?

4
  • 1
    This is wrong for sure if mname=="CS1TFD22" or "cs1tfd22":, should be if mname in ("CS1TFD22" ,"cs1tfd22"): Commented May 1, 2019 at 5:45
  • 1
    Possible duplicate of How to test multiple variables against a value? Commented May 1, 2019 at 5:45
  • What is the code supposed to do? Please include that explanation in your code too! Commented May 1, 2019 at 5:47
  • Correct your first if statement like this if mname=="CS1TFD22" or mname=="cs1tfd22": Commented May 1, 2019 at 5:47

4 Answers 4

2

Your if statement is always evaluating true, as you are evaluating the logical true/false of a string. You should use

if mname=="CS1TFD22" or mname=="cs1tfd22":

or

if mname in ("CS1TFD22","cs1tfd22"):
Sign up to request clarification or add additional context in comments.

2 Comments

Your never which character would be Upper or Lower Case. So its better to use casefold()
I actually didn't even notice that they were the same string in a different case! In that case yes, should definitely use casefold()
1

Some errors in your code

  1. You check for two or conditions not by if mname=="CS1TFD22" or "cs1tfd22":, but by if mname=="CS1TFD22" or mname=="cs1tfd22":, also you can simplify this by doing if mname.lower()=="cs1tfd22": .

  2. You don't need the extra elif in if l1l2=="NULL":, just else would do .

  3. You missed a single quote in icode = CS1'+m[5]+m[6]+'S003d13_L1_mh_'
  4. You have not defined li2 anywhere, not sure where you are taking it from, so I am taking it as input in my code

The code with all these issues might look like so

mname=input("Enter name: ")
l1l2=input("Enter l1l2")
m=[]

#Convert mname to lower case and then compare
if mname.lower() == 'cs1tfd22':
    mcode='CS122S003'

    #If-else case 1
    if l1l2=="NULL":
        icode = 'CS122S003d13_mh_'
    else:
        icode = 'CS122S003d13_L1_mh_'

else:
    for i in mname:
        m.append(i)
    mcode = 'CS1'+m[5]+m[6]+'S003'

    # If-else case 1
    if l1l2=="NULL":
        icode='CS1'+m[5]+m[6]+'S003d113_mh_'
    else:
        icode = 'CS1'+m[5]+m[6]+'S003d13_L1_mh_'

print(mcode,icode)

Some outputs from your code will be.

Enter name: cs1tfd22
Enter l1l2:  NULL
CS122S003 CS122S003d13_mh_

Enter name: abcdefgh
Enter l1l2:  NULL
CS1fgS003 CS1fgS003d113_mh_

Enter name: xyzabcd
Enter l1l2:  HELLO
CS1cdS003 CS1cdS003d13_L1_mh_

9 Comments

` also you can simplify this by doing if mname.lower()=="CS1TFD22":` this is wrong, should be mname.upper()
Damn! I put it correctly in the code but incorrectly in the example! I corrected them at both places! thanks @Netwave Please take a look again and if it looks good, please consider upvoting
its ok now, gj :)
What's gj! I don't know this!
Aah damn! Still learning the ropes of SO so any feedback is always appreciated @Netwave Especially from a high rep user like you :)
|
1

Instead of

if mname=="CS1TFD22" or "cs1tfd22":

use

 if mname=="CS1TFD22" or mname=="cs1tfd22":

2 Comments

or you can use casefold() also
this is correct. "cs1tfd22" allways evaluates to True, so the else condition is never hit
0
  • You should define l1l2 variable.
  • Use else instead elif.
  • Don't forget ' mark before CS1 at the last line.

    mname=input("Enter  name: ")
    m=[]
    l1l2=input("Enter  l1l2: ")
    
    if mname=="CS1TFD22" or "cs1tfd22":
        mcode='CS122S003'
        if l1l2=="NULL":
            icode = 'CS122S003d13_mh_'
        else:
            icode = 'CS122S003d13_L1_mh_'
    else:
        for i in mname:
            m.append(i)
    
        mcode = 'CS1'+m[5]+m[6]+'S003'
        if l1l2=="NULL":
            icode='CS1'+m[5]+m[6]+'S003d113_mh_'
        else:
            icode = 'CS1'+m[5]+m[6]+'S003d13_L1_mh_'
    
    print(mcode,icode)
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.