print 'L2 PROBLEM 11' print '===============' statemet=''' Assume that two variables, varA and varB, are assigned values, either numbers or strings. Write a piece of Python code that prints out one of the following messages: "string involved" if either varA or varB are strings "bigger" if varA is larger than varB "equal" if varA is equal to varB "smaller" if varA is smaller than varB For problems such as these, do not include raw_input statements or define the variable varA or varB. Our automating testing will provide values of varA and varB for you - so write your code in the following box assuming varA and varB are already defined.''' print statemet varA="34" varB="34" if type(varA) is str or type(varB) is str: print "string involved" elif varA > varB: print "bigger" elif varA == varB: print "equal" elif varA < varB: print "smaller" note='''\n\nNOTE:\n Is type(varA) == str or type(varB) == str equivalent to type(varA) or type(varB) == str ? Those are not equivalent because of Python precedence (some operations have higher precedence than others). The == has higher precedence than the or so it will get evaluated first. Therefore: type(varA) or type(varB) == str Will evaluate to the following, if we explicitly put parentheses: type(varA) or ( type(varB) == str ) True or ( type(varB) == str ) ( True ) Because "anything" or "True" will just take the value of "True" (by boolean algebra). And the other expression: type(varA) == str or type(varB) == str Will evaluate to the following, if we explicitly put parentheses: ( type(varA) == str ) or ( type(varB) == str ) So you will have to check each of the expressions in the parentheses to see whether they are true or not to determine the final result. So these two are not equal. ''' print note print "\n\nTeacher's code:" if type(varA) == str or type(varB) == str: print 'string involved' elif varA > varB: print 'bigger' elif varA == varB: print 'equal' else: # If none of the above conditions are true, # it must be the case that varA < varB print 'smaller'
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question