Advertisement
igendel

Humminboard i2eX Pyhton Guess The Number

Aug 27th, 2014
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. # Guess The Number game
  2. # For the HummingBoard-i2eX
  3. # by Ido Gendel, 2014
  4. # Share and enjoy!
  5.  
  6. import random
  7.  
  8. # if you set this to True, you'll need to run under sudo
  9. # or arrange the appropriate permissions for the user
  10. useGPIO = True
  11.  
  12. lLEDPath = "/sys/class/gpio/gpio72/"
  13. hLEDPath = "/sys/class/gpio/gpio73/"
  14.  
  15. def writeToFile(fileName, data):
  16.     f = open(fileName, "w")
  17.     f.write(data)
  18.     f.close()
  19.  
  20. def feedback(msg, lLED, hLED):
  21.     print msg
  22.     if useGPIO:
  23.         writeToFile(lLEDPath + "value", str(lLED))
  24.         writeToFile(hLEDPath + "value", str(hLED))
  25.  
  26. # Initialize GPIO as output
  27. if useGPIO:
  28.     writeToFile(lLEDPath + "direction", "out")
  29.     writeToFile(hLEDPath + "direction", "out")
  30.  
  31. print "Welcome to Guess The Number!"
  32. random.seed()
  33.  
  34. while True:
  35.  
  36.     feedback("I'm picking a number between 1 and 1000.", 0, 0)
  37.     n = random.randint(1, 1000)
  38.     print "Can you guess what it is?"
  39.  
  40.     guessed = False
  41.     guesses = 0
  42.     while not guessed:
  43.  
  44.         while True:
  45.             s = raw_input("Enter your guess (0 to quit): ")
  46.             if s.isdigit():
  47.                 break
  48.             else:
  49.                 print "Sorry, I don't understand '" + s + "'"
  50.  
  51.         guesses += 1
  52.         i = int(s)
  53.         if i == 0:
  54.             feedback("See you later.", 0, 0)
  55.             break    
  56.         elif i < n:
  57.             feedback("Too low.", 1, 0)
  58.         elif i > n:
  59.             feedback("Too high.", 0, 1)
  60.         else:
  61.             feedback("Correct!", 1, 1)
  62.             print "It took you " + str(guesses) + " guesses."
  63.             guessed = True
  64.          
  65.     if not guessed:
  66.         break
  67.  
  68.     s = raw_input("Type 'Y' if you want to play again... ").lower()
  69.     if s != "y":
  70.         feedback("Come again soon!", 0, 0)
  71.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement