#stuff the first person sees def get_secret(word): """Get the secret word/phrase""" input = raw_input(word) Words_so_far = "_ " * len(word) Misses = 0 max_wrong = 5 #Stuff the second person sees word = raw_input("Enter the secret word:") display = len(word) print display print word print "\n"*10 print "_ " * display def do_turn(display,misses): """ Display the current status for the user and let them make a guess. """
... while True: letter = do_turn if letter in puzzle and letter not in already_used: add_to_puzzle else: increase_misses print message ...
# Code version 1 def get_secret(): """Get the secret word/phrase""" input = raw_input(word) # etc. # test get_secret word = get_secret() print word
# Code version 2 def get_secret(): """Get the secret word/phrase""" input = raw_input(word) # etc. def do_turn(display, misses): print display # etc. # test get_secret word = get_secret() print word # test do_turn display = "_ETTE_" misses = 2 letter = do_turn(display, misses) print letter
)
write "Enter the secret word" . . . write guess a letter while misses is less than 6 and guess is not equal to secret word write guess a letter if letter guessed is in secret word, then add letter to display and replace underline else: increase misses by 1 print display if letters guessed equals secret word print congratulations, you got the secret word
#stuff the first person sees def get_secret(secret): """Get the secret word/phrase""" guess = raw_input(secret) #Stuff the second person sees def do_turn(display,misses): """ Display the current status for the user and let them make a guess. """ misses=0 guess = raw_input("What letter would you like to guess?") while misses < 6 and guess!=secret: for i in range(len(secret)): if guess != secret[i]: misses = misses +1 print str(misses) display = "_ " * len(secret) print display guess = get_secret(secret) print guess letter = do_turn(display,misses) print letter
get secret word initialize misses to 0 initialize guess to "_"*len(secret word) while misses is less than 6 and guess is not equal to secret word write guess a letter if letter guessed is in secret word, then add letter to display and replace underline else: increase misses by 1 print display if letters guessed equals secret word print congratulations, you got the secret word else: print sorry
# create the function -- this code gets stored, but not executed. def new_display(secret_word, old_guess, letter): # flip the secret word and the guess: turn A into _ in secret word and _ into A in guess. while True: if letter in secret_word: index = secret_word.find(letter) secret_word = secret_word.replace(letter, "_",1) old_guess = old_guess[:index]+letter+old_guess[index+1:] else: break return old_guess # call the function for testing purposes # initialize variables misses = 0 guess = "_E__E_" secret = "LETTER" # call the function with single letter letter = "R" guess = new_display(secret, guess, letter) # check the output if guess == "_E__ER": print "R: Test passed" else: print "R: Test failed" # call again with double letter letter = "T" guess = new_display(secret, guess, letter) # check output if guess == "_ETTER": print "T: Test passed" else: print "T: Test failed" # call with letter not in puzzle letter = "Z" guess = new_display(secret, guess, letter) # check output if guess == "_ETTER": print "Z: Test passed" else: print "Z: Test failed"
secret_word = secret_word.replace(letter, "_",1). You might think that this would cause the secret_word to get changed permanently in the main code. But actually, the function receives a copy of the secret_word, and when the function returns, all of its variables "go out of scope" -- that is, are forgotten.guess = new_display(secret, guess, letter); otherwise, those changes will be lost.
#stuff the first person sees def get_secret(secret): """Get the secret word/phrase""" secret = raw_input(secret) misses = 0 guess = "_ "*len(secret) print guess #Stuff the second person sees def do_turn(display,misses): """ Display the current status for the user and let them make a guess. """ guess = raw_input("What letter would you like to guess?") while misses < 6 and guess!=secret: for i in range(len(secret)): if guess != secret[i]: misses = misses +1 print str(misses) display = "_ " * len(secret) print display guess = get_secret(secret) print guess letter = do_turn(display,misses) print letter
Your code is improving.
## BAD ## >>> def get_secret(): """Get the secret word/phrase""" secret = raw_input("Enter the secret word: ") >>> misses = 0 >>> get_secret() Enter the secret word: phlerm >>> guess = "_"*len(secret) Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> guess = "_"*len(secret) NameError: name 'secret' is not defined >>>
## GOOD ## >>> def get_secret(): secret = raw_input("Enter the secret word: ") return secret # <<< 1 >>> misses = 0 >>> my_secret = get_secret() # <<< 2 Enter the secret word: phlerm >>> guess = "-"*len(my_secret) >>> print guess ------ >>> print my_secret phlerm >>>
# Define all your functions FIRST before the main code. #stuff the first person sees def get_secret(): """Get the secret word/phrase""" secret = raw_input("Enter the secret word: ") return secret #Stuff the second person sees # This function was trying to do the whole game. Your prof's directions gave you # the design for this function. It does one thing: get the user's guess. def do_turn(display, misses): """ Display the current status for the user and let them make a guess. """ print display print "You have missed %d times." % misses guess = raw_input("What letter do you guess? ") return guess misses = 0 secret = get_secret() print secret display = "-"*len(secret) letter = do_turn(display,misses) print letter
# Define all your functions FIRST before the main code. #stuff the first person sees def get_secret(): """Get the secret word/phrase""" secret = raw_input("Enter the secret word: ") return secret #Stuff the second person sees # This function was trying to do the whole game. Your prof's directions gave you # the design for this function. It does one thing: get the user's guess. def do_turn(display, misses): """ Display the current status for the user and let them make a guess. """ return display print "You have missed %d times." % misses #This is where we can update the letters guessed def new_display(secret, display, guess): """Displays the replacement of underscores with letters that the user has guessed""" misses = 0 while True: if guess in secret: index = secret.find(guess) secret = secret.replace(letter, "_",1) display = display[:index]+guess+display[index+1:] else: misses = misses+1 return secret,display misses = 0 secret = get_secret() print secret display = "-"*len(secret) letter = do_turn(display,misses) print letter secret,display = new_display(secret,display,guess) print secret,display
# Define all your functions FIRST before the main code. #stuff the first person sees def get_secret(): """Get the secret word/phrase""" secret = raw_input("Enter the secret word: ") return secret #Stuff the second person sees # This function was trying to do the whole game. Your prof's directions gave you # the design for this function. It does one thing: get the user's guess. def do_turn(display, misses): """ Display the current status for the user and let them make a guess. """ return display print "You have missed %d times." % misses #This is where we can update the letters guessed def new_display(secret, display, guess): """Displays the replacement of underscores with letters that the user has guessed""" misses = 0 while True: for i in range (len(secret)): if secret[i] == guess: print guess else: misses = misses+1 return secret,display misses = 0 secret = get_secret() print secret display = "-"*len(secret) letter = do_turn(display,misses) print letter secret,display = new_display(secret,display,guess) print secret,display
#This is where we can update the letters guessed def new_display(secret, display, guess): """Displays the replacement of underscores with letters that the user has guessed""" misses = 0 while True: for i in range (len(secret)): if secret[i] == guess: print guess else: misses = misses+1 return secret,display
| DaniWeb Message | |
| Cancel Changes | |