如果语句满足,但代码不运行

时间:2018-06-12 15:53:00

标签: python python-3.x

在我的代码中,似乎if语句要求匹配它运行的两个变量似乎相等,但它被跳过。

# This program will play the classic game of "Hangman", with a certain amount of times you can fail, in the end telling the user if they have failed or succeeded in guessing the word
# Creating variables for the number of times players can guess and establishing lists for storing letters they have guessed and lists for what they have successfully guessed so far.
lives = 6
word = 'banana'
listGuess = []
wordAsList = list(word)
blanks = '_'*len(word)
blankList = list(blanks)
blankReference = list(blanks)
print('Word: ')
#Printing the blanks in accordance with the number of letters in the word. 
print(' '.join(blanks) +
      '\n')
#Creating a loop that allows the player to continue guessing, (or failing).
while lives != 0 :
    guess = input('Your guess: ')
#Creating certain things the user cannot input.    
    if len(guess.lower()) > 1 :
        print('\n' + 'Try entering only one character.')
        print(' '.join(blankList) + '\n')
    elif guess.lower() == '' or guess.lower() == ' ' :
        print('\n' + 'Try entering a character, you know. From the alphabet.')
        print(' '.join(blankList) + '\n')
    elif guess.lower() in listGuess :
        print('\n' + 'You\'ve already guessed that letter, genius. -1 life.')
        lives = lives - 1
        print('You have', lives, 'lives remaining.')
        print(' '.join(blankList) + '\n')
#Replacing the black dashes with the succefully guessed letter, in a list by comparing it to a list of guessed letters.
    else :
        listGuess.append(guess)
        count = 0
        for letter in word :
            if guess == wordAsList[count] :
                blankList[count] = wordAsList[count]
            count = count + 1
#Telling the user whether their guess was in the word and showing them an updated version of the blanks, containing guessed characters if availible. Also finds if the user successfully guessed the word, or if they failed.
        if blankList == blankReference :
#ERROR HAPPENING HERE^
            print('\n' + 'Bad luck. Your guess was not in the word.')
            lives = lives - 1
            print('You have', lives, 'tries remaining.')
            print(' '.join(blankList) + '\n')
        elif word != ''.join(blankReference) :
            count = 0
            for item in blankReference :
                item = blankList[count]
                count = count + 1
            if ''.join(blankList) == word :
                print('\n' + ' '.join(blankList))
                print('Congratulations, you have successfully guessed the word and saved the convicted man.')
                quit()
            else :
                print('\n' + 'Your guess was in the word!')
                print(' '.join(blankList) + '\n')
print('Bad luck! The man is dead. ')

我当前的例子是' banana',显然但是当我输入b时,然后d - 它输出猜测在单词中。

Code example

2 个答案:

答案 0 :(得分:1)

我对你的一些代码感到有些困惑:

count = 0
for letter in word :
    if guess == wordAsList[count] :
        blankList[count] = wordAsList[count]
    count = count + 1

看起来好像我们在这个单词上循环并收到每个字母,但我们从未使用过这封信。

此外,通过将计数设置为零,我们只有在我们猜到单词的第一个字母时才会更正,尽管我看到您稍后再加1,所以这可能是有意的。

您已经发现问题列表的位置,但逻辑存在声音。如果由于blankList不等于blankReference而进一步向下的问题,我们流入elif,然后打印出你看到的内容。

我尝试尽可能保持代码尽可能接近您自己,希望它可以正常工作,您可以看到任何其他差异。

lives = 6
word = 'banana'
listGuess = []
wordAsList = list(word)
blanks = '_'*len(word)
blankList = list(blanks)
print(' '.join(blanks) +
      '\n')
while lives != 0 :
    guess = input('Your guess: ')
    if len(guess.lower()) > 1:
        print('\n' + 'Try entering only one character.')
        print(' '.join(blankList) + '\n')
    elif guess.lower() == '' or guess.lower() == ' ':
        print('\n' + 'Try entering a character, you know. From the alphabet.')
        print(' '.join(blankList) + '\n')
    elif guess.lower() in listGuess:
        print('\n' + 'You\'ve already guessed that letter, genius. -1 life.')
        lives = lives - 1
        print('You have', lives, 'lives remaining.')
        print(' '.join(blankList) + '\n')
    else:
        listGuess.append(guess)
        correct_guess = False
        for i in range(len(word)):
            if guess == wordAsList[i] :
                blankList[i] = wordAsList[i]
                blankList_new = blankList
                correct_guess = True


        if correct_guess == False:
            print('\n' + 'Bad luck. Your guess was not in the word.')
            lives = lives - 1
            if lives == 0:
                print('Bad luck! The man is dead. ')
            else:
                print('You have', lives, 'tries remaining.')
                print(' '.join(blankList) + '\n')
        elif word == ''.join(blankList):
            print('\n' + ' '.join(blankList))
            print('Congratulations, you have successfully guessed the word and saved the convicted man.')
            quit()
        else:
            print('\n' + 'Your guess was in the word!')
            print(' '.join(blankList) + '\n')

希望这有帮助! :)

答案 1 :(得分:0)

# This program will play the classic game of "Hangman", with a certain amount of times you can fail, in the end telling the user if they have failed or succeeded in guessing the word
# Creating variables for the number of times players can guess and establishing lists for storing letters they have guessed and lists for what they have successfully guessed so far.
lives = 6
val=0
word = 'banana'
listGuess = []
wordAsList = list(word)
blanks = '_'*len(word)
blankList = list(blanks)
blankReference = list(blanks)
print('Word: ')
#Printing the blanks in accordance with the number of letters in the word. 
print(' '.join(blanks) +
      '\n')
#Creating a loop that allows the player to continue guessing, (or failing).
while lives != 0 :
    guess = input('Your guess: ')
#Creating certain things the user cannot input.    
    if len(guess.lower()) > 1 :
        print('\n' + 'Try entering only one character.')
        print(' '.join(blankList) + '\n')
    elif guess.lower() == '' or guess.lower() == ' ' :
        print('\n' + 'Try entering a character, you know. From the alphabet.')
        print(' '.join(blankList) + '\n')
    elif guess.lower() in listGuess :
        print('\n' + 'You\'ve already guessed that letter, genius. -1 life.')
        lives = lives - 1
        print('You have', lives, 'lives remaining.')
        print(' '.join(blankList) + '\n')
#Replacing the black dashes with the succefully guessed letter, in a list by comparing it to a list of guessed letters.
    else :
        listGuess.append(guess)
        count = 0
        for letter in word :
            if guess == wordAsList[count] :
                blankList[count] = wordAsList[count]
                val=val+1
            count = count + 1
#Telling the user whether their guess was in the word and showing them an updated version of the blanks, containing guessed characters if availible. Also finds if the user successfully guessed the word, or if they failed.
        if val == 0 :
#ERROR HAPPENING HERE^
            print('\n' + 'Bad luck. Your guess was not in the word.')
            lives = lives - 1
            print('You have', lives, 'tries remaining.')
            print(' '.join(blankList) + '\n')
        elif word != ''.join(blankReference) :
            count = 0
            for item in blankReference :
                item = blankList[count]
                count = count + 1
            if ''.join(blankList) == word :
                print('\n' + ' '.join(blankList))
                print('Congratulations, you have successfully guessed the word and saved the convicted man.')
                quit()
            else :
                print('\n' + 'Your guess was in the word!')
                print(' '.join(blankList) + '\n')
        val=0
print('Bad luck! The man is dead. ')

在我的电脑上测试它的工作....我希望这将有助于你:)