Advertisement
AarellanoH

spell_spanish

Feb 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. class spell_spanish:
  2.     def __init__(self):
  3.         pass
  4.    
  5.     #get a random letter that is not contained in the word passed as an argument
  6.     def get_random_letter(self, current_word = "", uppercase = True):
  7.         letter_alphabet = ascii_uppercase
  8.         if(uppercase == False):
  9.             letter_alphabet = ascii_lowercase
  10.         while True:
  11.             random_letter = random.choice(letter_alphabet)
  12.             if(current_word.find(random_letter) < 0):
  13.                 break
  14.         return random_letter
  15.  
  16.     #get a random letter that is not contained in the word passed as an argument
  17.     def get_random_letter(self, current_word = "", uppercase = True):
  18.         letter_alphabet = ascii_uppercase
  19.         if(uppercase == False):
  20.             letter_alphabet = ascii_lowercase
  21.         while True:
  22.             random_letter = random.choice(letter_alphabet)
  23.             if(current_word.find(random_letter) < 0):
  24.                 break
  25.         return random_letter
  26.  
  27.     #get a string with four different letters, the first one (passed as parameter) is the correct choice
  28.     #if uppercase is false, the four letters will be lowercase, otherwrise, they will be uppercase
  29.     def get_four_letters(self, correct_letter, uppercase = True):
  30.         if(uppercase == True):
  31.             correct_letter = correct_letter.upper()
  32.         else:
  33.             correct_letter = correct_letter.lower()
  34.         four_letters = correct_letter
  35.         for i in range (1,4):
  36.             new_letter = self.get_random_letter(four_letters, uppercase)
  37.             four_letters += new_letter
  38.         return four_letters
  39.  
  40.     #make random 4 letters of each letter of a word
  41.     #TODO change len(word)-1 back to 0
  42.     def randomize_word(self, word, uppercase = True):
  43.         if(uppercase):
  44.             word = word.upper()
  45.         else:
  46.             word = word.lower()
  47.         for i in range (0, len(word)):
  48.             four_letters = self.get_four_letters(word[i],uppercase)
  49.             list_of_characters = list(four_letters)
  50.             random.shuffle(list_of_characters)
  51.             four_scrambled_letters = ''.join(list_of_characters)
  52.             correct_letter_index = four_scrambled_letters.find(word[i])
  53.             self.show_letter_options(four_scrambled_letters)
  54.             self.correct_input(correct_letter_index)
  55.         clear_screen()
  56.         display_message("FELICIDADES!")
  57.  
  58.         #wait for the user to input the correct option (0-3)
  59.     def correct_input(self, correct_letter_index):
  60.         while True:
  61.             button1 = GPIO.input(6)
  62.             button2 = GPIO.input(13)
  63.             button3 = GPIO.input(19)
  64.             button4 = GPIO.input(26)
  65.             if(button1):
  66.                 time.sleep(0.2)
  67.                 if(correct_letter_index == 0):
  68.                     print("Correcto")
  69.                     break
  70.                 else:
  71.                     print("Incorrecto")
  72.  
  73.             elif(button2):
  74.                 time.sleep(0.2)
  75.                 if(correct_letter_index == 1):
  76.                     print("Correcto")
  77.                     break
  78.                 else:
  79.                     print("Incorrecto")
  80.             elif(button3):
  81.                 time.sleep(0.2)
  82.                 if(correct_letter_index == 2):
  83.                     print("Correcto")
  84.                     break
  85.                 else:
  86.                     print("Incorrecto")
  87.             elif(button4):
  88.                 time.sleep(0.2)
  89.                 if(correct_letter_index == 3):
  90.                     print("Correcto")
  91.                     break
  92.                 else:
  93.                     print("Incorrecto")
  94.  
  95.     def show_letter_options(scrambled_letters):
  96.         matrix.display_letter(0,scrambled_letters[0])
  97.         matrix.display_letter(1,scrambled_letters[1])
  98.         matrix.display_letter(2,scrambled_letters[2])
  99.         matrix.display_letter(3,scrambled_letters[3])
  100.  
  101.     def run(self):
  102.         textToSpeech.say("Deletrea Hola")
  103.         self.randomize_word("Hola")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement