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

2024-06-26 13:54:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中,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. ')

我现在的例子是'香蕉',显然,但似乎当我输入b,然后d-它输出的猜测是在这个词。你知道吗

Code example


Tags: oftheinforifhavecountword
2条回答

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

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

看起来好像我们在循环这个词,得到每个字母,但我们从来没有用过那个字母。你知道吗

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


你已经发现了问题列表的位置,但逻辑是合理的。问题是,如果再往下看,由于blankList不等于blankReference,我们进入elif,然后else打印出您看到的内容。你知道吗

我试着让代码尽可能接近您自己的代码,希望它可以工作,您可以看到任何额外的差异。你知道吗

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')

希望这有帮助!:)

# 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. ')

在我的电脑上测试它工作。。。。我希望这能帮助你:)

相关问题 更多 >