Last active
December 17, 2019 01:59
-
-
Save lfalanga/f5d8573bef7cc873f253f73e522468a2 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
| # Example 1: Simple While / Else loop | |
| import random | |
| print "Lucky Numbers! 3 numbers will be generated." | |
| print "If one of them is a '5', you lose!" | |
| count = 0 | |
| while count < 3: | |
| num = random.randint(1, 6) | |
| print num | |
| if num == 5: | |
| print "Sorry, you lose!" | |
| break | |
| count += 1 | |
| else: | |
| print "You win!" | |
| # Example 2: Guess a random number from 1 to 10 | |
| from random import randint | |
| # Generates a number from 1 through 10 inclusive | |
| random_number = randint(1, 10) | |
| guesses_left = 3 | |
| # Start your game! | |
| while guesses_left > 0: | |
| guess = int(raw_input("Your guess: ")) | |
| if guess == random_number: | |
| print "You win!" | |
| break | |
| guesses_left -= 1 | |
| else: | |
| print "You lose." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment