python - Function variables: Whats the range of scope of a function -


recently have been working on word game in python, nonetheless having problems little detail.the idea of game create words letters in hand given randomly. function coordinates game asks user input letter , according it, lets him/her play new hand, play same hand again or exit game. problem cannot make game return previous hand , let user play again. instead code returns modified version of previous hand, if inputted word correctly letters in word won't in it. thought not possible since function "updates hand" lies on different scope. working in python 3.2.1

i posted entire code test , see does. problem, however, lies in functions: play_game, play_hand , update_hand.

import random import string  vowels = 'aeiou'  consonants = 'bcdfghjklmnpqrstvwxyz'  hand_size = 7  scrabble_letter_values = {     'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10 }  wordlist_filename = "words.txt"  def load_words():      """     returns list of valid words. words strings of lowercase letters.      depending on size of word list, function may     take while finish.     """     print ("loading word list file...")     # infile: file     infile = open(wordlist_filename, 'r')     # wordlist: list of strings     wordlist = []     line in infile:         wordlist.append(line.strip().lower())     print ("  ", len(wordlist), "words loaded.")     return wordlist  def get_frequency_dict(sequence):      """     returns dictionary keys elements of sequence     , values integer counts, number of times     element repeated in sequence.      sequence: string or list     return: dictionary     """     # freqs: dictionary (element_type -> int)     freq = {}     x in sequence:         freq[x] = freq.get(x,0) + 1     return freq   def get_word_score(word, n):      """     returns score word. assumes word     valid word.      score word sum of points letters     in word multiplied length of word, plus 50     points if n letters used on first go.      letters scored in scrabble; worth 1, b     worth 3, c worth 3, d worth 2, e worth 1, , on.      word: string (lowercase letters)     returns: int >= 0     """     score = 0     character in word:         score = score + (scrabble_letter_values[character])     score = score * len(word)     if len(word) == 7:         score = score + 50     return score   def display_hand(hand):      """     displays letters in hand.      example:        display_hand({'a':1, 'x':2, 'l':3, 'e':1})     should print out like:        x x l l l e     order of letters unimportant.      hand: dictionary (string -> int)     """     letter in hand.keys():         j in range(hand[letter]):              print (letter,)                def deal_hand(n):      """     returns random hand containing n lowercase letters.     @ least n/3 letters in hand should vowels.      hands represented dictionaries. keys     letters , values number of times     particular letter repeated in hand.      n: int >= 0     returns: dictionary (string -> int)     """     hand={}     num_vowels = int(n / 3)      in range(num_vowels):         x = vowels[random.randrange(0,len(vowels))]         hand[x] = hand.get(x, 0) + 1      in range(num_vowels, n):             x = consonants[random.randrange(0,len(consonants))]         hand[x] = hand.get(x, 0) + 1      return hand   def update_hand(hand, word):      """     assumes 'hand' has letters in word.     in other words, assumes many times     letter appears in 'word', 'hand' has @ least     many of letter in it.       updates hand: uses letters in given word     , returns new hand, without letters in it.      has no side effects: not modify hand.      word: string     hand: dictionary (string -> int)         returns: dictionary (string -> int)     """     in word:         new_hand = hand         new_vowels = vowels         new_consonants = consonants         if in vowels:             new_vowels = new_vowels.replace("i", "")             new_hand[i] = new_hand.get(i, 0) - 1             if new_hand[i] <= 0:                 new_hand.pop(i, none)          else:             new_consonants = new_consonants.replace("i", "")             new_hand[i] = new_hand.get(i, 0) - 1             if new_hand[i] <= 0:                 new_hand.pop(i, none)     return (new_hand)   def is_valid_word(word, hand, word_list):      """     returns true if word in word_list , entirely     composed of letters in hand. otherwise, returns false.     not mutate hand or word_list.      word: string     hand: dictionary (string -> int)     word_list: list of lowercase strings     """      character in word:         x = 0         if character in hand:             x = x + 1     if (x==(len(word)-1)) , (word in word_list):         return (true)     else:         return (false)  def calculate_handlen(hand):     handlen = 0     v in hand.values():         handlen += v     return handlen   def play_hand(hand, word_list):      """     allows user play given hand, follows:      * hand displayed.      * user may input word.      * invalid word rejected, , message displayed asking       user choose word.      * when valid word entered, uses letters hand.      * after every valid word: score word displayed,       remaining letters in hand displayed, , user       asked input word.      * sum of word scores displayed when hand finishes.      * hand finishes when there no more unused letters.       user can finish playing hand inputing single       period (the string '.') instead of word.        hand: dictionary (string -> int)       word_list: list of lowercase strings      """     score = 0     han = hand     while true:         print ("score= ", score)         word = str(input("enter word: "))         value = is_valid_word(word, han, word_list)         if value == true:             print ("congratulations, word worth", get_word_score(word, 7), "points")             print ("please input word")             han = update_hand(han, word)             print ("current hand=")             display_hand(han)             score = score + get_word_score(word, 7)         elif word == "." or len(hand)==0:             break         else:             print ("current hand=")             display_hand(han)             print ("sorry, word not valid")             print ("please choose word")    def play_game(word_list):      """     allow user play arbitrary number of hands.      * asks user input 'n' or 'r' or 'e'.      * if user inputs 'n', let user play new (random) hand.       when done playing hand, ask 'n' or 'e' question again.      * if user inputs 'r', let user play last hand again.      * if user inputs 'e', exit game.      * if user inputs else, ask them again.     """     global handy      handy = deal_hand(7)     while true:         print ("use 'n' play new hand, 'r' play last hand again or 'e' exit game")         inp = str(input("enter letter:'n' or 'r' or 'e': ="))         if inp == 'n':             hand = deal_hand(7)             handy = hand             print("current hand =")             display_hand(hand)             play_hand(hand, word_list)         elif inp == 'r':             print("current hand =")             display_hand(handy)             play_hand(handy, word_list)         elif inp == 'e':             exit(play_game)         else:             print ("please input valid letter")  if __name__ == '__main__':     word_list = load_words()     play_game(word_list) 

writing y = x makes y refer to same thing x. not copy x. "immutable" objects, looks copy because have make new object in order "change" evident. "mutable" objects lists, because x , y both refer same object, changes 1 name appear in other.

you want copy list:

new_hand = hand[:] # funny slicing notation produces shallow copy of original list 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -