“Guess the number” Game in Python


One of the simplest two-player games is “Guess the number”. The first player thinks of a secret number in some known range while the second player attempts to guess the number. After each guess, the first player answers either “Higher”, “Lower” or “Correct!” depending on whether the secret number is higher, lower or equal to the guess. In this project, you will build a simple interactive program in Python where the computer will take the role of the first player while you play as the second player.

You will interact with your program using an input field and several buttons. For this project, we will ignore the canvas and print the computer’s responses in the console. Building an initial version of your project that prints information in the console is a development strategy that you should use in later projects as well. Focusing on getting the logic of the program correct before trying to make it display the information in some “nice” way on the canvas usually saves lots of time since debugging logic errors in graphical output can be tricky.

# 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
import simplegui
import random

# initialize global variables used in your code
number = random.randrange(0, 10); 
count = 0
maxNumber = 100;

# helper function to start and restart the game
def new_game():
    global number, count, maxNumber
    number = random.randrange(0, maxNumber);
    if(maxNumber == 100):
        count = 7
    else:
        count = 11   
    print ""
    print "New Game. Range is from 0 to " + str(maxNumber)
    print "Number of remaining guesses is " + str(count)   

# define event handlers for control panel
def range100():
    # button that changes range to range [0,100) and restarts
    global count
    count = 100
    new_game()
    
def range1000():
    # button that changes range to range [0,1000) and restarts
    global count
    count = 1000
    new_game()
    
def input_guess(guess):
    # main game logic goes here
    global number, count
    
    print ""
    print "Guess was " + guess 
    print "Number of remaining guesses is " + str(count)
    
    if( count == 0):
        print "You ran out of guesses. The number is "+str(number)
        new_game() 
        return 
    count = count - 1
    if(int(guess)<number):
        print  "Higher"
    elif(int(guess)>number):
        print "Lower"
    else:
        print "Correct!"
        new_game() 
    
# create frame
frame = simplegui.create_frame('Guess the number', 200, 300)


# register event handlers for control elements
frame.add_button('Range is [0 ,100)', range100,150)
frame.add_button('Range is [0 ,1000)', range1000,150)
frame.add_input('Enter a guess', input_guess, 150)


# call new_game and start frame
new_game()


# always remember to check your completed program against the grading rubric

Rock-paper-scissors-lizard-Spock in Python


Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously “throw” one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the rules:

  • Rock smashes scissors
  • Scissors cuts paper
  • Paper covers rock

Rock-paper-scissors is a surprisingly popular game that many people play seriously (see the Wikipedia article for details). Due to the fact that a tie happens around 1/3 of the time, several variants of Rock-Paper-Scissors exist that include more choices to make ties less likely.

Rock-paper-scissors-lizard-Spock (RPSLS) is a variant of Rock-paper-scissors that allows five choices. Each choice wins against two other choices, loses against two other choices and ties against itself. Much of RPSLS’s popularity is that it has been featured in 3 episodes of the TV series “The Big Bang Theory”. The Wikipedia entry for RPSLS gives the complete description of the details of the game.

# Rock-paper-scissors-lizard-Spock 

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

# convert name to number using if/elif/else
def name_to_number(name):
    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:
        print "ERROR Name"
# convert number to a name using if/elif/else
def number_to_name(number):
    
    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:
        print "ERROR Number"
def rpsls(player_choice): 

    
    # print a blank line to separate consecutive games
    print "\n"
    
    # 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
    difference = (comp_number - player_number) % 5
    
    # use if/elif/else to determine winner, print winner message
    if( difference == 1 or difference == 2 ):
        print "Computer wins!"
    elif ( difference == 4 or difference == 3 ):
        print "Player wins!"
    elif( difference == 0 ):
        print "Player and computer tie!"
        
           
# test your code - LEAVE THESE CALLS 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