igendel icon

Humminboard i2eX Pyhton Guess The Number

igendel | PRO | 08/27/14 01:05:59 PM UTC | 0 ⭐ | 1064 👁️ | Never ⏰ | []
Python |

1.78 KB

|

None

|

0 👍

/

0 👎

# Guess The Number game
# For the HummingBoard-i2eX
# by Ido Gendel, 2014
# Share and enjoy!
 
import random
 
# if you set this to True, you'll need to run under sudo
# or arrange the appropriate permissions for the user
useGPIO = True
 
lLEDPath = "/sys/class/gpio/gpio72/"
hLEDPath = "/sys/class/gpio/gpio73/"
 
def writeToFile(fileName, data):
    f = open(fileName, "w")
    f.write(data)
    f.close()
 
def feedback(msg, lLED, hLED):
    print msg
    if useGPIO:
        writeToFile(lLEDPath + "value", str(lLED))
        writeToFile(hLEDPath + "value", str(hLED))
 
# Initialize GPIO as output
if useGPIO:
    writeToFile(lLEDPath + "direction", "out")
    writeToFile(hLEDPath + "direction", "out")
 
print "Welcome to Guess The Number!"
random.seed()
 
while True:
 
    feedback("I'm picking a number between 1 and 1000.", 0, 0)
    n = random.randint(1, 1000)
    print "Can you guess what it is?"
 
    guessed = False
    guesses = 0
    while not guessed:
 
        while True:
            s = raw_input("Enter your guess (0 to quit): ")
            if s.isdigit():
                break
            else:
                print "Sorry, I don't understand '" + s + "'"
 
        guesses += 1
        i = int(s)
        if i == 0:
            feedback("See you later.", 0, 0)
            break    
        elif i < n:
            feedback("Too low.", 1, 0)
        elif i > n:
            feedback("Too high.", 0, 1)
        else:
            feedback("Correct!", 1, 1)
            print "It took you " + str(guesses) + " guesses."
            guessed = True
         
    if not guessed:
        break
 
    s = raw_input("Type 'Y' if you want to play again... ").lower()
    if s != "y":
        feedback("Come again soon!", 0, 0)
        break

Comments