Reputation: 49
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?
Upvotes: 1
Views: 724
Reputation: 624
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)
Upvotes: 0
Reputation: 20490
Some errors in your code
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":
.
You don't need the extra elif
in if l1l2=="NULL":
, just else
would do .
icode = CS1'+m[5]+m[6]+'S003d13_L1_mh_'
li2
anywhere, not sure where you are taking it from, so I am taking it as input in my codeThe 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_
Upvotes: 1
Reputation: 177
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"):
Upvotes: 2
Reputation: 333
Instead of
if mname=="CS1TFD22" or "cs1tfd22":
use
if mname=="CS1TFD22" or mname=="cs1tfd22":
Upvotes: 1