Hello Everyone,
I need someone to guide me to create a function that loop through 2 words and return matching letters as an integer. I am making a guessing game and I don’t expect anyone to do my code, I just need some suggestions.
Suppose we have word_1 and word_2 as ‘Hello’ and ‘Hi’ , and word_1 is the password and word_2 is the guess. In order to give a hint to user I need the function to return numbers of matching letters.
Please note user will input an integer not the words itself.
So far I got,
def compare(word_1 ,word_2):
matched=0
for letter in range(len(word1)):
if letter == range(len(word2)):
matched+=1
matched.append(letter)
return matched
I need someone to guide me to create a function that loop through 2 words and return matching letters as an integer.
Your function looks good! So, not sure what kind of guidance you’re seeking? My version:
Use List Comprehension which returns iterable, then returns the length.
def compare(word_1, word_2):
return len([i for i in word_1 if i in word_2])
Use Set to cast both word_1 and word_2 to Set data type, then perform intersection:
def compare(word_1, word_2):
return len(set(word_1).intersection(set(word_2)))
Please note user will input an integer not the words itself.
Not too clear what this means?
Thanks for your time replying to my question
I meant that I have enumerated my list of possible correct answer(let’s say 7 words) , and the user will input the number of the corresponding word not the word itself.
Ah I see, that’s a nice guessing game
thank you so much
could you please help me with this as well?
My loop does not end even if the guess is correct. maybe I have done my values in correctly?
guess= None
if guess!= password:
** won= False**
elif guess==password:
** won= True**
while guess_remaining>0 and won==False:
** print (‘The password is one of these words:’)**
** guess=(int(input(‘Please Make your guess by entering the corresponding word number:’)))**
** for index, words in enumerate(word_list, 1):**
** print(index, words)**
** if guess!= password:**
** guess_remaining-=1**
** won= False**
** print(‘Incorrect guess, you still have’, guess_remaining, ‘guesses(s) remaining’)**
** elif guess==password:**
** print(‘Spot on, you, You guessed it while you had’, guess_remaining, ‘guesses remained’)**
** won= True**
Hello! Could you please format your code correctly? See this post, as it preserves whitespace. What is the current output for your code?
Sure can
password= random.choice(word_list)
guess= None
if guess!= password:
won= False
elif guess==password:
won= True
while guess_remaining>0 and won==False :
print ('The password is one of these words:')
for index, words in enumerate(word_list, 1):
print(index, words)
guess=(int(input('Please Make your guess by entering the corresponding word number:')))
if guess==password:
print('Spot on, you, You guessed it while you had', guess_remaining, 'guesses remained')
won==True
elif guess!= password:
guess_remaining-=1
print('Incorrect guess, you still have', guess_remaining, 'guesses(s) remaining')
won==False
The issue is here:
==
is an equality operator, which means it checks for equality. So here, all you’re doing is going does won equal True?
.