Hangman program with python -
i need make hangman program python program don't know how continue. in program given 6 chances, otherwise known “life line”, guess letters in word. every wrong guess shorten “life line”. game end when guess word correctly or have used “life line”. here sample output:
['_', '_', '_', '_'] ***life line*** -+-+-+-+-+-+ $|$|$|$|$|$| -+-+-+-+-+-+ enter guess: ['_', '_', '_', '_'] ***life line*** -+-+-+-+-+ $|$|$|$|$| -+-+-+-+-+ incorrect guess: ['a'] ................................... enter guess: b ['b', '_', '_', '_'] ***life line*** -+-+-+-+-+ $|$|$|$|$| -+-+-+-+-+ incorrect guess: ['a'] ................................... enter guess: l ['b', 'l', '_', '_'] ***life line*** -+-+-+-+-+ $|$|$|$|$| -+-+-+-+-+ incorrect guess: ['a'] ................................... enter guess: o ['b', 'l', '_', '_'] ***life line*** -+-+-+-+ $|$|$|$| -+-+-+-+ incorrect guess: ['a', 'o'] ................................... enter guess: u ['b', 'l', 'u', '_'] ***life line*** -+-+-+-+ $|$|$|$| -+-+-+-+ incorrect guess: ['a', 'o'] ................................... enter guess: e ['b', 'l', 'u', 'e'] ***life line*** -+-+-+-+ $|$|$|$| -+-+-+-+ incorrect guess: ['a', 'o'] ................................... got right! done! i have typed in first few codes got stuck.
import random wordlist = ["mary","tian pei","pong"] randname = random.choice ( wordlist) print randname resultlist = [ ] in range(len(randname)): resultlist.append("_") print resultlist
create blank list:
>>> name = "mary" >>> blanks = ["_" letter in name] >>> blanks ['_', '_', '_', '_'] create list of incorrect guesses:
>>> incorrect_guesses = # figure 1 out set life-lines:
>>> life_lines = # figure 1 out prompt guess:
>>> guess = raw_input("guess: ") guess: >>> guess 'a' save variable says whether or not guess incorrect:
>>> incorrect = # figure 1 out iterate on name , replace respective blank line in blanks letter if respective letter in name same guess:
>>> in range(len(name)): ... if name[i] == guess: ... incorrect = # figure 1 out ... blanks[i] = # figure 1 out ... >>> blanks ['_', 'a', '_', '_'] if incorrect true, add guess incorrect_guesses (in case, since guess correct, wouldn't update) , subtract life-line:
>>> if incorrect: ... incorrect_guesses.append( # figure 1 out ) ... life_lines -= # figure 1 out ... to check equivalence, join letters in blanks re-form original word can compare two:
>>> final = ''.join(blanks) # connects each letter empty string >>> final '_a__' the general control structure following:
choose random word create blanks list set life-lines while life_lines greater 0 , word not completed: print blanks list print life_lines graphic if there incorrect guesses: print incorrect guesses prompt guess check if correct , fill in blanks if incorrect: add incorrect guesses , subtract life-line if word completed: win else: lose it's fill in blanks wrote here (and write own routines printing life-line graphics , such).
Comments
Post a Comment