If statement error in Python -


there seems recurring error in python code... problem presume within if statement, there possibility me? doing assignment @ school , appears if user guesses word, congratulated , if don't it, have try again.

my code:

import time import random import os  words = open("words.txt","r") wordlist = []  lines in words:   wordlist.append(lines)  wordlist=[line.rstrip('\n')for line in wordlist]   print(wordlist[0:3]) print(wordlist[3:6]) print(wordlist[6:9]) time.sleep(2) os.system(['clear','cls'][os.name == 'nt'])  random.shuffle(wordlist)  print(wordlist[0:3]) print(wordlist[3:6]) print(wordlist[6:9])  removedword = (wordlist[9])  print("---------------------------")  guesses = 0  while guesses <3: guess = input("what removed word?") guesses = guesses + 1    if guess == removedword:       print("you have guessed correctly!")    else:       print("fail") 

inside shell:

['night', 'smoke', 'ghost'] ['tooth', 'about', 'camel'] ['brown', 'funny', 'chair'] ['tooth', 'brown', 'chair'] ['price', 'smoke', 'funny'] ['about', 'night', 'camel'] --------------------------- removed word?ghost have guessed correctly! removed word?ghost have guessed correctly! removed word?ghost have guessed correctly! 

your while-loop counts 3 , doesn't stop though answer correct. avoid need check if answer correct , break loop.

here's altered code:

import time import random import os  words = open("words.txt","r") wordlist = []  lines in words:     wordlist.append(lines)     wordlist=[line.rstrip('\n')for line in wordlist]   print(wordlist[0:3]) print(wordlist[3:6]) print(wordlist[6:9]) time.sleep(2) os.system(['clear','cls'][os.name == 'nt'])  random.shuffle(wordlist)  print(wordlist[0:3]) print(wordlist[3:6]) print(wordlist[6:9])  removedword = (wordlist[9]) #printed win every time #print(removedword)  print("---------------------------")  guesses = 0 #added flag  unanswered = true  #while guesses less 3 , question unanswered  while guesses <3 , unanswered:     guess = input("what removed word?")     guesses = guesses + 1     if guess == removedword:         print("you have guessed correctly!")         #correct answer, changes flag         unanswered = false     else:         print("fail")  if unanswered:     print("game over!") 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -