简体   繁体   中英

How to only make a chunk of code active 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

I have no idea about your level of coding tho I would try to use a

if or contain for example

x = input("char player entered")
chr = [sun, tree]
if x.__contains__(x):
    chr.remove('sun')

you can use a while loop to restart the game or re-append new words to the list another way is to create a brand new list that contains the word the person chose

x = input("char player entered")
chr = [sun, tree]
n = []
if x.__contains__(x):
    z = chr.index(x)
    n.append(chr[z])

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