Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 12, 2011 15:09
Show Gist options
  • Save dketov/1467724 to your computer and use it in GitHub Desktop.
Save dketov/1467724 to your computer and use it in GitHub Desktop.
Ветвления
# -*- encoding: utf-8 -*-
"""
Базовый синтаксис
"""
x = int(raw_input("Please enter an integer: "))
if x < 0:
x = 0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
# -*- encoding: utf-8 -*-
"""
Составное условие
"""
number = 1
if number <= 10 and number >= 1:
print 'Great!'
else:
print 'Wrong!'
# -*- encoding: utf-8 -*-
"""
Стандартный селектор
"""
choice = 'ham'
print {'spam': 1.25, # a dictionary-based 'switch'
'ham': 1.99, # use has_key() or get() for default
'eggs': 0.99,
'bacon': 1.10}[choice]
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'
# -*- encoding: utf-8 -*-
"""
Вложеные условия
"""
x = 1
if x:
y = 2
if y:
print 'block2'
print 'block1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment