Skip to content

Instantly share code, notes, and snippets.

@dketov
Created December 12, 2011 15:30
Show Gist options
  • Save dketov/1467862 to your computer and use it in GitHub Desktop.
Save dketov/1467862 to your computer and use it in GitHub Desktop.
Циклы с условием
# -*- encoding: utf-8 -*-
"""
Базовый синтаксис
"""
x = 1
while x <= 100:
print x
x += 1
# -*- encoding: utf-8 -*-
"""
Прерывание цикла
"""
while 1:
word = raw_input('Please enter a word: ')
if not word:
break
print 'The word was ' + word
# -*- encoding: utf-8 -*-
"""
Цикл с завершением
"""
y = 10
x = y / 2 # for some y > 1
while x > 1:
if y % x == 0: # remainder
print y, 'has factor', x
break # skip else
x = x-1
else: # normal exit
print y, 'is prime'
# -*- encoding: utf-8 -*-
"""
Угадай моё число
"""
import random
# set the initial values from 1 to 5
the_number = random.randrange(5) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
# guessing loop
while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment