scrabble_letter_values = {'a':5, 'b':15, 'v':15, 'q':10, 'l':15} hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1} word_list = ['cat', 'dog', 'quail', 'quailml', 'bone', 'ml'] score = 0 ##--------------test values above----------------------------## #gets value for individual letter, checking for missing values def get_letter_score(letter): return scrabble_letter_values.get(letter, 0) def get_word_score(word, n, score): ##base case if word == '': if n == 7: score = score + 50 return score ##recursive case else: score = score + get_letter_score(word[len(word)-1]) word = word[0:len(word)-1] return get_word_score(word, n, score) #displays the current letters in the hand as a string def display_hand(hand): handstring = '' for i in hand: for x in range(hand[i]): handstring = str(handstring + ' ' + i.upper()) if handstring != '': print 'Letters in hand:', handstring else: print 'Empty hand!' ##Checks a word to make sure the letters required are in the hand def check_hand(hand, word): for i in word: inWord = hand.get(i, 0) if inWord == 0: return False else: return True ##updates and removes letters in a hand once used in a word def update_hand(hand, word): for i in word: if hand[i]-1 == 0: del hand[i] else: hand[i] = hand[i]-1 ##function checks to ensure that words are in in word_list and that letters in word are in hand def is_valid_word(word, hand, word_list): if word in word_list and check_hand(hand,word) == True: return True elif word not in word_list and check_hand(hand, word) == True: print 'Not a word!' elif word in word_list and check_hand(hand, word) == False: print 'You do not have one or more of those letters in your hand!' else: print 'You do not have one or more of those letters! And that is not a word!' ##checks if word is valid, scores word, then removes letters from hand def play_hand(hand, score): word = '' while word != '.': display_hand(hand) #word = 'quail' word = raw_input('Enter word or a "." to indicate that you are finished!: ') if word == '.': break if is_valid_word(word, hand, word_list) == True: update_hand(hand, word) score = score + get_word_score(word, len(word), score) play_hand(hand, score) print score play_hand(hand, score)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question