I have been trying to code a hangman game but couldn’t do it because there seems to be an error with my if requests, because he dosn’t detect a valid Letter in the word although there is .
please tell me if i can give you any more information. looking forward to hearing from yall and i am really interested in the skills i can learn:D
import random
import string
def check_guess(guess):
try:
str(guess)
return True
except:
return False
def check_lvl(difficulty):
try:
int(difficulty)
return True
except:
return False
def ask_for_difficulty(force_valid_input):
while True:
difficulty = input("Please choose your difficulty Level, starting from level 1 to 3: ")
if check_lvl(difficulty) and difficulty == '1' or difficulty == '2' or difficulty == '3':
return int(difficulty)
else:
if not check_lvl(difficulty):
return None
print("wrong input")
def get_words():
print("Loading word list from file...")
f = open("/home/hiighq/Desktop/VisualCode/hangman-python-shirelkatz/countries-and-capitals.txt", 'rb', 0)
content = f.readline
wordlist = []
for content in f:
wordlist.append(content.strip())
print (" ", len(wordlist), "words loaded.")
f.close
return wordlist
def choose_word(wordlist):
return str(random.choice(wordlist))
wordlist = get_words()
word_to_guees = choose_word(wordlist)
pos_of_line = word_to_guees.rfind("|")
pos_of_point = word_to_guees.rfind("'")
secretWord = word_to_guees[pos_of_line+2:pos_of_point]
def is_word_guessed(secretWord, lettersGuessed):
count = 0
for i, c in enumerate(secretWord):
if c in lettersGuessed:
count += 1
if count == len(secretWord):
return True
else:
return False
def get_guessed_word(secretWord, lettersGuessed):
count = 0
blank = ["_ "] * len(secretWord)
for i, c in enumerate(secretWord):
if c in lettersGuessed:
count += 1
blank.insert(count-1,c)
blank.pop(count)
if count == len(secretWord):
return ''.join(str(e)for e in blank)
else:
count +=1
blank.insert(count-1, '_')
blank.pop(count)
if count == len(secretWord):
return ''.join(str(e) for e in blank)
def get_availible_letters(lettersGuessed):
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
alphabet2 = alphabet[:]
def removeDupsBetter(L1, L2):
L1Start = L1[:]
for e in L1:
if e in L1Start:
L2.remove(e)
return ''.join(str(e) for e in L2)
return removeDupsBetter(lettersGuessed, alphabet2)
def set_difficulty(difficulty):
lives = int
if difficulty == 1:
lives = 8
print("At level 1, you will have ",(lives), "lives!")
return lives
elif difficulty == 2:
lives = 5
print("At level 2, you will have ",(lives), "lives!")
return lives
elif difficulty == 3:
lives = 3
print("At level 3, you will have ",(lives), "lives!")
return lives
def hangman(secretWord):
intro = len(str(secretWord))
lettersGuessed= []
guess = str
mistakesMade = 0
wordGuessed = False
difficulty = int
print("Welcome to the hangman game!")
difficulty = ask_for_difficulty(True)
lives = set_difficulty(difficulty)
print("-----------------------------------------------")
print("I am thinking of a word with ",intro," letters!",secretWord)
while lives > 0 and mistakesMade <= lives and wordGuessed is False:
if secretWord == get_guessed_word(secretWord, lettersGuessed):
wordGuessed = True
break
print("You have",str(lives)," guesses left.")
print("Available letters: ", get_availible_letters(lettersGuessed))
guess = input("Please guess a letter: ").lower
if str(guess) in secretWord: #checks if input is in secretword
if str(guess) in lettersGuessed:
print("Looks like you have already tried this letter: ", get_availible_letters(lettersGuessed))
print("-----------------------------------------------")
else:
lettersGuessed.append(guess)
print("Good guess: ", get_guessed_word(secretWord,lettersGuessed))
else:
if str(guess) in lettersGuessed:
print("Looks like you have already tried this letter: ", get_availible_letters(lettersGuessed))
print("-----------------------------------------------")
else:
lettersGuessed.append(guess)
lives -= 1
mistakesMade += 1
print("That letter is not in the word! You have ", lives," left.",get_guessed_word(secretWord,lettersGuessed))
if wordGuessed == True:
return ("Congratz you won!:D")
elif lives == 0:
print("You ran out of lives. the word was", secretWord)
hangman(secretWord)
Advertisement
Answer
The problem arises in your removeDupsBetter method. You are trying to remove an element from L2 which is not present in L2.
Maybe you actually meant to do this:
def removeDupsBetter(L1, L2):
L1Start = L1[:]
for e in L1:
// Checking if element is present in L2 before removing it.
if e in L2:
L2.remove(e)
return ''.join(str(e) for e in L2)