how to make game connect a word?, i was confuse to check the character
my code:
lenght_first_word = int(input('Lenght first word: '))
first_word = input('First Word: ')
lenght_second_word = int(input('Lenght second word: '))
second_word = input('Second word: ')
first_word_spelling = [*first_word]
second_word_spelling = [*second_word]
list_word = []
repeat = -1
y = 0
while y < lenght_second_word:
for i in range(lenght_second_word, -1, -1):
if first_word_spelling[-1] == second_word_spelling[repeat]:
list_word.append(second_word_spelling[repeat])
else:
repeat -= 1
y = 1
print(list_word)
i hope the input is :
Input :
Lenght first word: 6
First word: center
Lenght second word: 9
Second word: terminate
Output : the second word can be connected with "ter"
==========Or==========
Input:
Lenght first word: 4
First word: show
Lenght second word: 3
Second word: why
Output : the second word can be connected with "w"
if the words can't be connected the output is "the word can't be connected"
thanks a lot:))
CodePudding user response:
Here is a simpler way to do it
first_word = input('First Word: ')
second_word = input('Second word: ')
joined = ""
for i in range(1, min(len(first_word), len(second_word)) 1):
if first_word[-i:] == second_word[:i]:
joined = first_word[-i:]
if joined:
print(f"The words can be connected with {joined}")
else:
print("Words cannot be connected")
Here we loop through the shorter word's length and compare the last 1, 2, 3... letters from word 1 to same amount of letters in the beginning of word 2.
There are a few pointers with your example I wanted to bring up.
- You don't need to query the length of the words from user, it's easier to determine it ourselves in the code using
len('word')
. - Casting strings as a list is redundant as well, since in python strings are already lists of characters and thus can be sliced as is.
- You don't need two loops, since you're always comparing the first x characters of the first word and second x characters from the second word. See my example.
- You have the index variable
i
free to use in the for loopfor i in range...:
, you don't need an extra variable to carry over just to add or subtract 1 in every loop. For the same reason, your first loop would work better as a for loop instead of a while loop.
CodePudding user response:
search for letter in second word by comparing first letter of first word and use indexing.
first_word = input('First Word: ')
second_word = input('Second word: ')
index_value = []
for ind, val in enumerate(first_word):
if val == second_word[0]:
index_value.append(ind)
if index_value:
search_word = first_word[index_value[0]:]
n = len(search_word)
if search_word == second_word[:n]:
print(f'the second word can be connected with "{search_word}"')
else:
print("the word can't be connected")
else:
print("the word can't be connected")
>>>> First Word: center
>>>> Second word: terminal
>>>> the second word can be connected with "ter"
CodePudding user response:
Hope this helps:
first_word = input('First Word: ')
second_word = input('Second word: ')
merge = 0
for i in range(1, min(len(first_word), len(second_word)) 1):
if first_word[-i:] == second_word[:i]:
merge = first_word[-i:]
if merge:
print("The second word can be connected with", str(merge))
else:
print("The word can't be connected")