#!/usr/bin/env python
import random
import RPi.GPIO as GPIO
import time
# generate a random number from 1 to 10
n = random.randint(1,10)
# keep running until number is guessed
while True:
    guess = raw_input('Guess of a number from 1 to 10')
    guess = int(guess)
    if guess < n:
        print "guess is too low"
        # breadboard setup 
        GPIO.setmode(GPIO.BOARD)
        GPIO.setwarnings(False)

        # assign pin number for passive buzzer; pin 32 = GPIO 12
        buzz_pin = 32

        # set Passive Buzzer pin's mode as output
        GPIO.setup(buzz_pin,GPIO.OUT)

        # create Buzz function and set initial sound frequency to 1000 Hz
        Buzz = GPIO.PWM(buzz_pin,1000)
        Buzz.ChangeFrequency(440)

        # start Passive Buzzer using Buzz function with 50% duty for 1 second
        Buzz.start(50)
        time.sleep(1)

        # stop Passive Buzzer using Buzz Function
        Buzz.stop()

        # reset GPIO resources used by this script
        GPIO.cleanup()
    elif guess > n:
        print "Guess is too high"
        # breadboard setup 
        GPIO.setmode(GPIO.BOARD)
        GPIO.setwarnings(False)

        # assign pin number for passive buzzer; pin 32 = GPIO 12
        buzz_pin = 32

        # set Passive Buzzer pin's mode as output
        GPIO.setup(buzz_pin,GPIO.OUT)

        # create Buzz function and set initial sound frequency to 1000 Hz
        Buzz = GPIO.PWM(buzz_pin,1000)
        Buzz.ChangeFrequency(440)

        # start Passive Buzzer using Buzz function with 50% duty for 1 second
        Buzz.start(50)
        time.sleep(1)

        # stop Passive Buzzer using Buzz Function
        Buzz.stop()

        # reset GPIO resources used by this script
        GPIO.cleanup()
    else: 
        # breadboard setup
        GPIO.setmode(GPIO.BOARD)
        GPIO.setwarnings(False)

        # assign pin number for Auto Flash LED: pin 11 = GPIO 17
        led_pin = 11

        # set Auto Flash LED pin's mode as output
        GPIO.setup(led_pin,GPIO.OUT)

        # turn on Auto Flash LED
        GPIO.output(led_pin,True)
        time.sleep(2)
        # turn off Auto Flash LED
        GPIO.output(led_pin,False)

        # reset GPIO resources used by this script
        GPIO.cleanup()
        print "you guessed it"
        break 
