Jump to content

SO I wrote a scripy ni python to unscramble scrambled words. It doesn't give errors but it does nothing.

I typed english words on google and got 58000 words. I pasted them into a txt file(myfile.txt). And check if input is one of the words.

here is the code:

wordsFile = open('myfile.txt', 'r') 
while(True):
	scrambledWord = input()
	while(True):
		words = wordsFile.readlines() 
		if len(scrambledWord) == len(words):
			for i in range(len(words)):
				wordsList.append(words[i])
				scrambledList.append(scrambledWord[i])
				wordsList.sort()
				scrambledList.sort()
				if wordsList == scrambledList:
					print(wordsList)
					break 

 

Link to comment
https://linustechtips.com/topic/1249425-unscramble-script/
Share on other sites

Link to post
Share on other sites

There are a couple of problems. First you should only call readlines() once, so move that out of the while loops. The first time it will give a list of lines in the text file, but the second and subsequent calls will return an empty list.

 

Second

if len(scrambledWord) == len(words):

 checks if the length of the word you input is equal to the length of the entire list of words you have in your text file. Unless I misunderstand that you are not inputting just one word into scambledWord, that's unlikely to ever be true if you feed it 58000 words in a file and is also never true as you call readlines multiple times and are hence comparing to a length of 0.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
https://linustechtips.com/topic/1249425-unscramble-script/#findComment-14042687
Share on other sites

Link to post
Share on other sites

9 minutes ago, Wictorian said:

SO I wrote a scripy ni python to unscramble scrambled words. It doesn't give errors but it does nothing.

I typed english words on google and got 58000 words. I pasted them into a txt file(myfile.txt). And check if input is one of the words.

here is the code:

You want to make an anagram finder?

Link to comment
https://linustechtips.com/topic/1249425-unscramble-script/#findComment-14042688
Share on other sites

Link to post
Share on other sites

21 minutes ago, tikker said:

There are a couple of problems. First you should only call readlines() once, so move that out of the while loops. The first time it will give a list of lines in the text file, but the second and subsequent calls will return an empty list.

 

Second


if len(scrambledWord) == len(words):

 checks if the length of the word you input is equal to the length of the entire list of words you have in your text file. Unless I misunderstand that you are not inputting just one word into scambledWord, that's unlikely to ever be true if you feed it 58000 words in a file and is also never true as you call readlines multiple times and are hence comparing to a length of 0.

OH ı Totally misunderstood readlines() then. How can I check one line at a time?

Link to comment
https://linustechtips.com/topic/1249425-unscramble-script/#findComment-14042712
Share on other sites

Link to post
Share on other sites

1 hour ago, tikker said:

There are a couple of problems. First you should only call readlines() once, so move that out of the while loops. The first time it will give a list of lines in the text file, but the second and subsequent calls will return an empty list.

 

Second


if len(scrambledWord) == len(words):

 checks if the length of the word you input is equal to the length of the entire list of words you have in your text file. Unless I misunderstand that you are not inputting just one word into scambledWord, that's unlikely to ever be true if you feed it 58000 words in a file and is also never true as you call readlines multiple times and are hence comparing to a length of 0.

when I print words at the end of the strings there is \n. how can I delete this? Now I edited it as you said but it still doesnt work and I think that may be the problem. I am also adding the new code.

a = -1
wordsList = []
scrambledList =[]
wordsFile = open('myfile.txt', 'r')
words = wordsFile.readlines()
print(words)
while(True):
	scrambledWord = input()
	while(True):
		a += 1
		word = words[a] 
		if len(scrambledWord) == len(word):
			for i in range(len(word)):
				wordsList.append(word[i])
				scrambledList.append(scrambledWord[i])
		wordsList.sort()
		scrambledList.sort()
		if wordsList == scrambledList:
			break 		

 

Link to comment
https://linustechtips.com/topic/1249425-unscramble-script/#findComment-14042805
Share on other sites

Link to post
Share on other sites

1 hour ago, Wictorian said:

when I print words at the end of the strings there is \n. how can I delete this? Now I edited it as you said but it still doesnt work and I think that may be the problem. I am also adding the new code.

<snip>

You can call the strip() method (strips whitespace and characters like newline) of the string or use read().splitlines() instead of readlines().

2 hours ago, Wictorian said:

OH ı Totally misunderstood readlines() then. How can I check one line at a time?

Have a look at iterating over lists using e.g. for loops.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
https://linustechtips.com/topic/1249425-unscramble-script/#findComment-14042895
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×