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

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -