Python,If tests

返回

>>> if 1: ... print 'true' ... true >>> >>> if not 1: ... print 'true' ... else: ... print 'false' ... false >>> x = 'killer rabbit' >>> if x == 'roger': ... print "how's jessica?" ... elsif x == 'bugs': File "<stdin>", line 3 elsif x == 'bugs': ^ SyntaxError: invalid syntax >>> elif x == 'bugs': File "<stdin>", line 1 elif x == 'bugs': ^ SyntaxError: invalid syntax >>> if x == 'roger': ... print "how's jessica?" ... elif x == 'bugs': ... print "what's up doc?" ... else: ... print 'Run away! Run away!' ... Run away! Run away! >>> >>> choice = 'ham' >>> print {'spam': 1.25, ... 'ham': 1.99, ... 'eggs': 0.99, ... 'bacon':1.10}[choice] 1.99 >>> if choice == 'spam': ... print 1.25 ... elif choice == 'ham': ... print 1.99 ... elif choice == 'eggs': ... print 0.99 ... elif choice == 'bacon': ... print 1.10 ... else File "<stdin>", line 9 else ^ SyntaxError: invalid syntax >>> else: File "<stdin>", line 1 else: ^ SyntaxError: invalid syntax >>> if choice == 'spam': ... print 1.25 ... elif choice == 'ham': ... print 1.99 ... elif choice == 'eggs': ... print 0.99 ... elif choice == 'bacon': ... print 1.10 ... else: ... print 'Bad choice' ... 1.99 >>>
返回