Python: Hangman game problems -
i brand new python have littel matlab , c++ background. please help!!!!
i having problems hangman code. if word has multiple of same letter in cannot figure out how switch of them. have couple tries of them commented out.
import random import time import sys def pickword(): words = [line.strip() line in open('word_list.txt')] word = random.choice(words) word = word.lower() return word def checkletter(word, input): if input not in word: in_letter = 0 else: in_letter = 1 return in_letter def check_input(input): if input.isaplha() == false : input = raw_input('your input not letter, please enter letter: ') elif len(input) > 0: input = raw_input('your entry longer 1 letter, please enter 1 letter: ') else: input = input return input #main function running = 'y' print('lets play hangman!\n\n ------------------------ \n\ngenerating random word \n\n') while running == 'y': word = pickword() letters = list(word) time.sleep(3) print ('the word has been chosen\n\n') print '%s' % word start = raw_input('are ready start?(y/n)') start = start.lower() if start == 'n': print('well, damn bad. here go!\n\n **************************\n\n') elif start == 'y': print('awesome, lets begin!\n\n*********************************\n\n') else: print('you did not enter y or n, sorry not allowed play!') sys.exit() = 0 print ('the word has %d letters in it') % len(word) input = raw_input('what first guess: ') input = input.lower() correct = ['_'] * len(word) print ' '.join(correct) while correct != letters , <= 5: '''if input in letters: input in letters: location = letters.index(input) correct[location] = input print('you guessed correct letter! input %s in word @ %d spot.') % (input, location) print ' '.join(correct) elif input not in letters: print('you guessed wrong :(') = + 1 guesses = 6 - print('you have %d guesses left.') % guesses guesses = 0 else: print('you did not enter valid letter, please try again.')''' '''for j in letters: if j == input: location = letters.index(j) correct[location] = j print '%s' % ' '.join(correct) print '%d' % location print '%s' % j if j == input: location = letters.index(j) correct[location] = j print('you guessed correct letter! input %s in word @ %d spot.') % (input, location) print ' '.join(correct)''' if input not in letters: = + 1 guesses = 6 - print("you guessed incorrectly. have %d guesses left.") % guesses input = raw_input('what next guess: ') input = input.lower() if correct == letters: print('congrats! won game!') else: print('you lost. :(') running = raw_input('do want play again? (y/n)').lower()
in attempt, loop stopping when finds first match of input letters.
the following code work:
guess = raw_input('what first guess: ') word = "wordword" letters = list(word) correct = ['_']* len(word) x, y in enumerate(word): if guess == y: correct[x] = y
your mistakes
in first attempt:
if input in letters: input in letters:
you checking if input in letters, fine, if returns true, inputs original value lost, , reassigned loop through elements of letters
.
eg
>>>input = "w" >>>word = "word" >>>if input in word: ... input in word: ... print(input) w o r d
your second attempt
for j in letters: if j == input: location = letters.index(j)
is lot closer being successful, location = letters.index(j)
going equal index of first match of j, , not assign matched values of input.
Comments
Post a Comment