lundi 30 novembre 2015

Using lists in if and else statements: Hangman as an example

Currently, I've been working on creating a hangman game and I've managed to get the majority of the scripts working and running perfectly however, I'm having problems with using lists in statements as I'd like to make a list of "used letters" which stops you from using the same letter again. The problem I've been finding is that I'll either set it up so it always includes the guess variable and not the input or it just doesn't work and I get a syntax error. Any suggestions on how I should do it?

Below, you'll find the code:

from random import randint
ans = input(">>>>>Let's play the word guessing game!<<<<<")
ans_upper = ans.upper()
if ans_upper == "YES":
    print("Good because that's what we're going to play!")
else:
    print("Tough, because we're going to play hangman anyways!")
#---#---#---#---#---#---#---#---#---#---# RANDOM WORD #---#---#---#---#---#---#---#
x = randint(1,15)
if x == 1:
    word = "bubblegum"
elif x == 2:
    word = "pasta"
elif x == 3:
    word = "cow"
elif x == 4:
    word = "chicken"
elif x == 5:
    word = "milk"
elif x == 6:
    word = "chair"
elif x == 7:
word = "computer"
elif x == 8:
    word = "psychology"
elif x == 9:
    word = "clock"
elif x == 10:
    word = "melon"
elif x == 11:
    word == "word"
elif x == 12:
    word = "brackets"
elif x == 13:
    word = "hangman"
elif x == 14:
    word = "paper"
elif x == 15:
    word = "internet"
else:
    print("ERROR: NUMBER HAS EXCEEDED THE VALUE OF 15")
#---#---#---#---#---#---#---#---#---# MYSTERY LETTERS #---#---#---#---#---#---#--
blanks = list("-"*len(word))
print("Can you guess the word, there are " , len(word), " letters!")
print(''.join(blanks))
word_list = list(word)
#---#---#---#---#---#---#---#---#---# LOOPING #---#---#---#---#---#---#---#---#---
loop = True
finished = len(word) * 1
done = 1
used = [] #This is for the used letters
while loop == True:
    guess = str(input("Enter a letter!"))
    checker = len(guess)
    if checker == 1:
        for letter_index in range(len(word_list)):
           if guess in word_list[letter_index]:
                guess_index = word_list.index(guess)
                letter_index = guess_index
                blanks[guess_index] = guess
                done = done + 1
                print("".join(blanks))

        if done == finished:
            print("You win!")
           loop = False
            break

    elif checker > 1:
        print("Hey, guess one letter at a time!")

So, the area that I'd like to have assigned to the task of storing the used letters us labeled as "used". I know that it involves the .append() command but I'm not sure where it'd go and how exactly I'd write out the command. Thanks in advance, Richard

Aucun commentaire:

Enregistrer un commentaire