简体   繁体   中英

How to only make a chunk of code working if a certain word is picked from a string

I am trying to create a hangman game using Python. Though, when the word "sun" is picked, and if the word "tree" is inputted into the console as an answer by me, it says the answer is correct when it is not. I have tried creating a function to fix this situation, though, it does not work for me...

Here is my code:

#hangman mini-project

import random
import string
import time

letters = string.ascii_letters
lettertree = ['a', 'b', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 's', 'u', 'v', 'w', 'x', 'y', 'z']
hangmanwords = ['tree','sun']
sunchoices = ['s _ _', '_ u _', '_ _ n']
treechoices = ['t _ _ _', '_ r _ _', ' _ _ e _', '_ _ _ e']
lettercount = [0]
gameWinTree = False
gameWinSun = False
limbCount = 5

hangmanword = random.choice(hangmanwords)
correct = hangmanword
if hangmanword == "sun":
    print (random.choice(sunchoices))
if hangmanword == "tree":
    print (random.choice(treechoices))
        
    
    if letters == "r":
        print("Nice! You guessed one letter from the word")
        if letters == "e":
            print("Wow! You guessed two letters from the word, if you wish, you can guess the word")
while True:
    letters = input("Please enter a letter to guess the word")
    if letters == "tree":
        input("Correct! The word was tree! Press enter to play again.")
        time.sleep(1)
        break
    if letters == "tree":
        gameWinTree == true
        if gameWinTree == true:
            time.sleep(1)
        break
    print("The letter that you chose was " + letters)
    if letters == "r":
        print("Nice! You guessed one letter from the word!\n t r _ _")
        
    if letters == "e":
        print("Wow! You guessed two letters from the word!\n t _ e e")
    if letters == "tree":
        print("Correct! The word was tree!")
    if letters == lettertree:
        print("Sorry, that's not a correct letter, feel free to try again.")
    limbCount -=1
    if limbCount == 0: 
        print("Unfortunately, you are out of tries, better luck next time!")
        time.sleep(1)
        exit()
       

Basically, if the word "sun" is picked, I want the code for the word tree to not work. Also sorry if my code is sloppy, tried to create this quickly! Thank you

Specifically for your problem

In your current code you are checking if the user guessed "tree" no matter what, while you actually only want to check this if the chosen word is "tree". So in your if statement you can add an "and" with an additional check for the correct word.

if letters == "tree" and correct == "tree":

More generalized

As already pointed out, no matter which word is chosen as the correct word, in your game loop you are always looking for the guess "tree".

You already saved a variable with the correct word to check for, so use that instead of checking for "tree":

if letters == "tree":

Should instead be:

if letters == correct:

And to then make the code write the proper message concat the strings in your print:

input("Correct! The word was " + correct + " Press enter to play again.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM