0
[SOLVED] Somebody please explain this :
>>> a = 'python' >>> ( a.upper() or a.lower() ) == 'PYTHON' True >>> ( a.lower() or a.upper() ) == 'PYTHON' False >>> ( a.upper() or a.lower() ) == 'python' False
14 Réponses
+ 7
Anna thanks for explanation
+ 5
Manish Kumar got it man
+ 3
Sam That's wrong. Whether a string contains a space or not doesn't affect that it will evaluate to True. Even a string that is nothing but a space (' ') will evaluate to True.
+ 3
Because when you add additional spaces the strings won't be equal. 'test ' is not the same as 'test'. But both 'test ' and 'test' evaluate to True.
+ 3
Manish Kumar Use "if a.lower() == 'add'". a.upper() can never equal an all lowercase string and you don't need any construct with "or" here
+ 2
#1.
>>> a = 'python'
>>> ( a.upper() or a.lower() ) == 'PYTHON'
True
a.upper () converts the value "a" to "PYTHON", if the first command is triggered before "or", then after "or" the command is ignored, which means "PYTHON" == "PYTHON".
#2.
>>> ( a.lower() or a.upper() ) == 'PYTHON'
False
a.lower() converts the value "a" to "python", if the first command is triggered before "or", then after "or" the command is ignored, which means "python" != "PYTHON".
#3.
>>> ( a.upper() or a.lower() ) == 'python'
False
With this code, I think you will figure it out yourself.
+ 1
The variable a contains a non-empty string ('python'). A non-empty string evaluates to True. If you compare several objects with "or", the first object that evaluates to True will be returned. So the result of a.upper() or a.lower() is 'PYTHON', the result of a.lower() or a.upper() is 'python'. The rest should be obvious
+ 1
sam, coz
>>>'python ' == 'python'
False
+ 1
Anna thanks.
0
Only the first function will be executed therefore only the second is true
0
Probably each line was executed .
0
Mr Anna , what command should I use in order to run a program which needs user input and that should not be case sensitive ?
0
#LANGUAGE = PYTHON 3.0
#SIMPLE CALCULATOR
# Its working fine.
while True:
print('*************************WELCOME***************************')
print('ENTER \'ADD\' FOR ADDITION.')
print('ENTER \'SUB\' FOR SUBTRACTION.')
print('ENTER \'MUL\' FOR MULTIPLICATION')
print('ENTER \'DIV\' FOR DIVISION.')
print('ENTER \'EXIT\' TO END/EXIT THE PROGRAM.')
a = input("ENTER ANY OF THE OPTIONS : ")
if (a.lower() or a.upper()) == 'add':
x = input('ENTER A NUMBER : ')
y = input('ENTER ANOTHER NUMBER : ')
print('ADDITION OF THE NUMBERS IS : ' + (str(float(x) + float(y))))
elif (a.lower() or a.upper()) == 'sub':
x = float(input('ENTER A NUMBER : '))
y = float(input('ENTER ANOTHER NUMBER : '))
print('SUBSTRACTION OF THE NUMBERS IS : ' + (str(float(x) - float(y))))
elif (a.lower() or a.upper()) == 'mul':
x = float(input('ENTER A NUMBER : '))
y = float(input('ENTER ANOTHER NUMBER : '))
print('PRODUCT OF THE NUMBERS IS : ' + (str(float(x) * float(y))))
elif (a.lower() or a.upper()) == 'div':
try:
x = float(input('ENTER A NUMBER : '))
y = float(input('ENTER ANOTHER NUMBER : '))
print('DIVISON OF THE NUMBERS IS : ' + (str(float(x) / float(y))))
except ZeroDivisionError:
print('0.0')
elif (a.lower() or a.upper()) == 'exit':
print('QUITTING THE PROGRAM.')
break
else :
print('POSSIBLY WRONG INPUT , TRY AGAIN.')