Last active
August 29, 2015 14:15
-
-
Save zfz/99f437ddd7fa63231b70 to your computer and use it in GitHub Desktop.
Mini Projets for An Introduction to Interactive Programming in Python Session 2
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
| # template for "Guess the number" mini-project | |
| # input will come from buttons and an input field | |
| # all output for the game will be printed in the console | |
| # Mini Project 2 for An Introduction to Interactive Programming in Python | |
| # URL http://www.codeskulptor.org/#user39_L4LKshd1tOatpPt_2.py | |
| import random | |
| import simplegui | |
| # helper function to start and restart the game | |
| def new_game(): | |
| # initialize global variables used in your code here | |
| global secret_number, i | |
| secret_number = random.randrange(0, 100) | |
| i = 7 | |
| # define event handlers for control panel | |
| def range100(): | |
| # button that changes the range to [0,100) and starts a new game | |
| global secret_number, i | |
| secret_number = random.randrange(0, 100) | |
| #print 'secret_number=', secret_number | |
| i = 7 | |
| def range1000(): | |
| # button that changes the range to [0,1000) and starts a new game | |
| global secret_number, i | |
| secret_number = random.randrange(0, 1000) | |
| #print 'secret_number=', secret_number | |
| i = 10 | |
| def input_guess(guess): | |
| # main game logic goes here | |
| guess = int(guess) | |
| print 'Guess was', guess | |
| global secret_number, i | |
| #print 'secret_number=', secret_number | |
| #print 'i=', i | |
| if i > 0: | |
| i -= 1 | |
| print "Number of remaining guesses is", i | |
| if guess == secret_number: | |
| print 'Correct!' | |
| elif guess < secret_number: | |
| print 'Higher.' | |
| else: | |
| print 'Lower.' | |
| else: | |
| print 'You have no guesses. Game Over.' | |
| # create frame | |
| frame = simplegui.create_frame('guess', 300,200) | |
| # register event handlers for control elements and start frame | |
| frame.add_button('new game', new_game) | |
| frame.add_button('range 0-100', range100) | |
| frame.add_button('range 0-1000', range1000) | |
| frame.add_input('Guess', input_guess, 50) | |
| frame.start() | |
| # call new_game | |
| new_game() | |
| # always remember to check your completed program against the grading rubric |
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
| # Mini Project 1 for An Introduction to Interactive Programming in Python | |
| # URL http://www.codeskulptor.org/#user39_g5rR1myKaGi8MJb.py | |
| # Rock-paper-scissors-lizard-Spock template | |
| import random | |
| # The key idea of this program is to equate the strings | |
| # "rock", "paper", "scissors", "lizard", "Spock" to numbers | |
| # as follows: | |
| # | |
| # 0 - rock | |
| # 1 - Spock | |
| # 2 - paper | |
| # 3 - lizard | |
| # 4 - scissors | |
| # helper functions | |
| def name_to_number(name): | |
| # delete the following pass statement and fill in your code below | |
| if name == "rock": | |
| return 0 | |
| elif name == "Spock": | |
| return 1 | |
| elif name == "paper": | |
| return 2 | |
| elif name == "lizard": | |
| return 3 | |
| elif name == "scissors": | |
| return 4 | |
| else: | |
| return None | |
| # convert name to number using if/elif/else | |
| # don't forget to return the result! | |
| def number_to_name(number): | |
| # delete the following pass statement and fill in your code below | |
| if number == 0: | |
| return "rock" | |
| elif number == 1: | |
| return "Spock" | |
| elif number == 2: | |
| return "paper" | |
| elif number == 3: | |
| return "lizard" | |
| elif number == 4: | |
| return "scissors" | |
| else : | |
| return None | |
| # convert number to a name using if/elif/else | |
| # don't forget to return the result! | |
| def rpsls(player_choice): | |
| # delete the following pass statement and fill in your code below | |
| # print a blank line to separate consecutive games | |
| print "" | |
| # print out the message for the player's choice | |
| print "Player Chooses " + player_choice | |
| # convert the player's choice to player_number using the function name_to_number() | |
| player_number = name_to_number(player_choice) | |
| # compute random guess for comp_number using random.randrange() | |
| comp_number = random.randrange(0, 4) | |
| # convert comp_number to comp_choice using the function number_to_name() | |
| comp_choice = number_to_name(comp_number) | |
| # print out the message for computer's choice | |
| print "Computer Chooses " + comp_choice | |
| # compute difference of comp_number and player_number modulo five | |
| win = (player_number - comp_number ) % 5 | |
| # use if/elif/else to determine winner, print winner message | |
| if win == 1 or win == 2: | |
| print "Player wins!" | |
| elif win == 3 or win == 4: | |
| print "Computer wins!" | |
| elif win == 0: | |
| print "Player and computer tie!" | |
| else: | |
| print "Error!" | |
| # test your code - THESE CALLS MUST BE PRESENT IN YOUR SUBMITTED CODE | |
| rpsls("rock") | |
| rpsls("Spock") | |
| rpsls("paper") | |
| rpsls("lizard") | |
| rpsls("scissors") | |
| # always remember to check your completed program against the grading rubric |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment