2015-06-19 3 views
-4

В моем коде Python появилась повторяющаяся ошибка ... Проблема, которую я предполагаю, заключается в заявлении if, есть ли возможность помочь мне? Я выполняю задание в школе, и кажется, что, если пользователь догадывается о слове, они получают поздравление, и если они этого не понимают, им приходится повторять попытку.Ошибка инструкции в Python

Мой код:

import time 
import random 
import os 

words = open("Words.txt","r") 
WordList = [] 

for 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 is the removed word?") 
guesses = guesses + 1 

    if guess == removedword: 
     print("You have guessed correctly!") 

    else: 
     print("Fail") 

Внутри оболочки:

['NIGHT', 'SMOKE', 'GHOST'] 
['TOOTH', 'ABOUT', 'CAMEL'] 
['BROWN', 'FUNNY', 'CHAIR'] 
['TOOTH', 'BROWN', 'CHAIR'] 
['PRICE', 'SMOKE', 'FUNNY'] 
['ABOUT', 'NIGHT', 'CAMEL'] 
--------------------------- 
What is the removed word?GHOST 
You have guessed correctly! 
What is the removed word?GHOST 
You have guessed correctly! 
What is the removed word?GHOST 
You have guessed correctly! 
+3

какая ошибка поживаешь? можете ли вы предоставить трассировку? –

+0

Не могли бы вы правильно упорядочить свой код и задать вопрос с оповещениями об ошибках. –

+1

Код делает именно то, что вы только что описали. - по дизайну, к чему вы это хотели, так как вы говорите свою ошибку? – Henrik

ответ

1

ваших подсчетов, а петлевых до 3 и не останавливается, даже если ваш ответ является правильным. Чтобы этого избежать, вам нужно проверить правильность ответа и разбить цикл.

Вот измененный код:

import time 
import random 
import os 

words = open("Words.txt","r") 
WordList = [] 

for 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 this so I could win every time 
#print(removedword) 

print("---------------------------") 

guesses = 0 
#Added flag 
unanswered = True 

#while guesses less than 3 and question is unanswered 
while guesses <3 and unanswered: 
    guess = input("What is the 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!") 
2
guesses = 0 
flag = 0 
while guesses <3: 
guess = input("What is the removed word?") 
guesses = guesses + 1 

if guess == removedword: 
    print("You have guessed correctly!") 
    flag = 1 
    break 
    else: 
    print("Fail") 

if flag == 0: 
    print("Game over") 
+0

Он будет печатать Fail 3 раза, а затем загружает третий раз Game Over !!!!! :) –

1
guesses = 0 

while guesses <3: 
    guess = input("What is the removed word?") 
    guesses = guesses + 1 

    if guess == removedword: 
     print("You have guessed correctly!") 
     break 
    else: 
     if guesses == 3: 
     print("Game over") 
     else: 
     print("Try again") 
Смежные вопросы