Here is my code:
import random guesses = 0 #Number of tries to guess. print ("Welcome to Guess Me!") print ("\nYou have five guesses, choose letter and guess a word.") print("\nAll of luck!") def rand_word(): f = open('sowpods.txt', 'r').readlines() #Sowpods is library with some random words return random.choice(f).strip() word = rand_word() print word #I printed a word just so i can test if is working. correct = word lenght = len(word) #I want to tell them how many letters a word contain new_length = str(lenght) guess = raw_input("The word is " + new_length + " letters long. Guess a letter: ") while guesses < 5: if guess not in word: print("Sorry, this letter is not in word.") else: print("Correct, word contain this letter!") guesses =+ 1 if guesses > 5: final_answer = raw_input("You ran out of guesses, what is your answer?") if final_answer == correct: print("That's correct, congratulations you won!") else: print("Sorry, correct word is" + " " + word + " ,you can try again!") Problem is when i run my code, and let's say i type letter "s", my sublime freeze and i get message "Sublime 3 is not responding.." and i have to turn it off. Maybe it's while loop? Infinite?
32 Answers
There are a few things.
guesses =+ 1 should be guesses += 1 and at the top of the while loop before any if statements I think your guess = raw_input should be within the while loop before any of the if statements. So it will keep asking and counting for each guess until it reaches 5. The last if statement within the loop should be if guesses == 5 or guesses >= 5 instead of guesses > 5.
Mostly just reordering things all within the while loop. Everything under the code within the while loop will be ran for every guess.
I changed it according to the above and it works great for me:
while guesses < 5: guesses += 1 guess = raw_input("The word is " + new_length + " letters long. Guess a letter: ") if guess not in word: print("Sorry, this letter is not in word.") else: print("Correct, word contain this letter!") if guesses == 5: final_answer = raw_input("You ran out of guesses, what is your answer?") if final_answer == correct: print("That's correct, congratulations you won!") else: print("Sorry, correct word is" + " " + word + " ,you can try again!") Welcome to Guess Me! You have five guesses, choose letter and guess a word. All of luck! word The word is 4 letters long. Guess a letter: 1 Sorry, this letter is not in word. The word is 4 letters long. Guess a letter: 2 Sorry, this letter is not in word. The word is 4 letters long. Guess a letter: 3 Sorry, this letter is not in word. The word is 4 letters long. Guess a letter: 4 Sorry, this letter is not in word. The word is 4 letters long. Guess a letter: 5 Sorry, this letter is not in word. You ran out of guesses, what is your answer?numbers Sorry, correct word is word ,you can try again! You have an infinite loop because your 'guesses' variable is not being inceremented in the while loop; move the 'guesses += 1' into the loop itself e.g.
while guesses < 5: if guess not in word: print("Sorry, this letter is not in word.") else: print("Correct, word contain this letter!") guesses += 1 0