Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's a slightly unconventional answer, but here's some edited code with commentary.</p> <pre><code>print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!") words= ['utopian','fairy','tree','monday','blue'] i=int(raw_input("Please enter any number to choose the word in the list: "))%5 #the %6 here means divide the input by six and take the remainder as the answer. I found the instructions a little confusing, and this allows the user to be competely unrestricted in the number that they choose while still giving you a number (0, 1, 2, 3, or 4) that you want. Increasing the number after the % will give you a larger variety of choices. # if(words[i]): #this line is unnecessary with the above code, and also would not prevent errors futher along in the code if i was not a number between 0 and 4. print "Your chosen word is", len(words[i]), "characters long." # slight wording change here #guess=input("Please enter the letter you guess: ") #if(guess in words[i]): #print("The letter is in the word.") #else: #print("The letter is not in the word.") #guesses=1 # all of this is already accomplished in the loop you wrote below. incorrect_guesses = 0 # it's always nice to initialize a variable while incorrect_guesses&lt;6: guess=str(raw_input("Please enter a letter to guess: ")) # Unless you're using python 3, I would use raw_input rather than input. if((guess) in words[i]): print('Correct! The letter "' + guess + '" appears in the word ' + str(words[i].count(str(guess))) + ' times.') else: print('Sorry, the letter "' + guess + '" is not in the word.') incorrect_guesses=incorrect_guesses+1 # Plusses instead of commas clean up the output a bit, and str(words[i].count(str(guess))) gives you the string form of the number which indicates how many times the guess appears in the word. This is useful in words like "tree". print("Failure. The word was:" , words[i]) # This is outside the while loop because it gets triggered as soon as incorrect guesses go over 6. # You can still improve the program by adding a feature which tells the players that they have guessed all of the correct letter and telling them what the word was, and possibly by increasing the word list. You can also tell players when they have already guessed a letter in case they've forgotten. </code></pre> <p>Hopefully this will be helpful to you in your future python endeavors.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload