mercredi 3 avril 2019

Is this a correct use of the in function in python?

I am trying to make a really basic hangman game by using an if/in function, however, the code wasn't responding to changes in the player input. Since this is my first time using the if/in a statement, I wanted to ask if this was right.

I tried a few other methods such as string find and individually making letters a string (that was a mess),the if/in seemed like the most efficient way of completing the task. If there are any other ways, please let me know.

put word into here, as a string :)

print("Welcome to Hangman") word = ["a", "v", "a", "c" "a", "d", "o"]

wordLength = len(word) + 1 lives = wordLength * 2

print("lives:", lives) print("Letters in word", wordLength)

guess = input("h")

while lives != 0: if guess in word: print("YES!") print(guess) index = word.index(guess) print(index) else: print("Wrong!") lives - 1 pass pass

while lives == 0: print("falied! try again") pass

The ideal results would be when the input matches a letter in the above string, the console would print "YES!" and the letter, or if not print "Wrong" and take away a life.

Aucun commentaire:

Enregistrer un commentaire