Created
December 12, 2011 15:30
-
-
Save dketov/1467862 to your computer and use it in GitHub Desktop.
Циклы с условием
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Базовый синтаксис | |
""" | |
x = 1 | |
while x <= 100: | |
print x | |
x += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Прерывание цикла | |
""" | |
while 1: | |
word = raw_input('Please enter a word: ') | |
if not word: | |
break | |
print 'The word was ' + word |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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