Jan-06-2018, 02:58 PM
Hi everyone
I'm quite new to python, only been doing it for a semester now, so I don't really now quite a lot of syntax, in fact, the syntax I use in the program of our interest today, may well be the only syntax I've fully mastered (that's why it's in the homework section). 'Fully' is a little ironic when posting on a forum ;), so let's get right into it. For a friend, who studies languages and likes to write poems, I tried to code a 'english word generator' that can 1. determine word length based on the normal distribution of the word lenghts in the English language or just use the word length that the user provided it with (this whole part works). And 2. Completely randomly build up a word from nothing (this part works too)(*). And 3. Checks if that word is a valid English word (I posses a digital list with all valid English words to check in), and if it is, print it to display. This last part is where the problems occur: the program runs perfectly natural, but instead of printing a word, it just doesn't print anything. In the variables list, I can see my word being a build-up of random characters, as long as the word length, but not nearly a valid word (it doesn't print, also). I understand that the chance of generating a valid English word this way(*) is pretty small, so I wondered if my computer might just stop trying it after a couple of hundred times. Is that a valid thought? Or should my computer keep running and generating? If it doesn't stop because of this, I don't see why it stops running at a certain point (it kind of 'gives up') because printing out a valid word is actually (at least that's what I think), the only way to stop for the program. I'll include the code in beneath.
Thanks in advance!
(*) I understand this way of 'building' a word might not be the most efficient way to do it (I could also just let the computer pick a random word from the list with valid words, even with the normal distribution (would be pretty much the same outcome and way less difficult for the computer)), but the idea that the computer 'composes' his own words just appeals very much to me! I can't stop thinking about the applications in artificial intelligence!
Extra information: I'm coding in Python 3 (IDE: Thonny) and my computer is a recent laptop with an i7 (7th gen).
Here's the failing code:
import random
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"]
word=""
counter=0
def isValid(word):{
file = open("wordsEn.txt", "r")
if word in file.read():
return True
else:
return False
}
wordlength=input("How many characters do you want the word to have? ")
if wordlength=="choose":{
ql=random.randint(1,1000)
if ql==1:{
wordlength=2}
if ql in range(2,7):{
wordlength=3}
if ql in range(8,33):{
wordlength=4}
if ql in range(34,85):{
wordlength=5}
if ql in range(86,170):{
wordlength=6}
if ql in range(171,292):{
wordlength=7}
if ql in range(293,432):{
wordlength=8}
if ql in range(433,572):{
wordlength=9}
if ql in range(573,698):{
wordlength=10 }
if ql in range(699,799):{
wordlength=11}
if ql in range(800,874):{
wordlength=12}
if ql in range(875,926):{
wordlength=13}
if ql in range(927,958):{
wordlength=14}
if ql in range(959,978):{
wordlength=15}
if ql in range(979,988):{
wordlength=16}
if ql in range(989,994):{
wordlength=17}
if ql in range(995,997):{
wordlength=18}
if ql in range(998,999):{
wordlength=19}
if ql==1000:{
wordlength=20}
}
else:{
wordlength=int(wordlength)
}
while counter<wordlength:{
word=word+alphabet[random.randint(0,25)]
if len(word)==wordlength:{
if isValid(word)==True:{
print("The generated word is: "+word)}
else:{
word=""
counter=0}
}
counter=counter+1
}
I haven't put all those brackets {} in the real file but I saw my indents didn't make it through to the layout on the threadpage, so I've added those to clarify.
On request, I can post the file I'm using with all the English words, but that might take up some space here, and I'm absolutely sure the file is correct (my teacher provided me with it in an earlier assignment and it worked perfectly). For the layout though, here are the first 10 lines:
a
aah
aahed
aahing
aahs
aardvark
aardvarks
aardwolf
ab
abaci
(You guys understand, it's really ALL English words ;))
I'm quite new to python, only been doing it for a semester now, so I don't really now quite a lot of syntax, in fact, the syntax I use in the program of our interest today, may well be the only syntax I've fully mastered (that's why it's in the homework section). 'Fully' is a little ironic when posting on a forum ;), so let's get right into it. For a friend, who studies languages and likes to write poems, I tried to code a 'english word generator' that can 1. determine word length based on the normal distribution of the word lenghts in the English language or just use the word length that the user provided it with (this whole part works). And 2. Completely randomly build up a word from nothing (this part works too)(*). And 3. Checks if that word is a valid English word (I posses a digital list with all valid English words to check in), and if it is, print it to display. This last part is where the problems occur: the program runs perfectly natural, but instead of printing a word, it just doesn't print anything. In the variables list, I can see my word being a build-up of random characters, as long as the word length, but not nearly a valid word (it doesn't print, also). I understand that the chance of generating a valid English word this way(*) is pretty small, so I wondered if my computer might just stop trying it after a couple of hundred times. Is that a valid thought? Or should my computer keep running and generating? If it doesn't stop because of this, I don't see why it stops running at a certain point (it kind of 'gives up') because printing out a valid word is actually (at least that's what I think), the only way to stop for the program. I'll include the code in beneath.
Thanks in advance!
(*) I understand this way of 'building' a word might not be the most efficient way to do it (I could also just let the computer pick a random word from the list with valid words, even with the normal distribution (would be pretty much the same outcome and way less difficult for the computer)), but the idea that the computer 'composes' his own words just appeals very much to me! I can't stop thinking about the applications in artificial intelligence!
Extra information: I'm coding in Python 3 (IDE: Thonny) and my computer is a recent laptop with an i7 (7th gen).
Here's the failing code:
import random
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"]
word=""
counter=0
def isValid(word):{
file = open("wordsEn.txt", "r")
if word in file.read():
return True
else:
return False
}
wordlength=input("How many characters do you want the word to have? ")
if wordlength=="choose":{
ql=random.randint(1,1000)
if ql==1:{
wordlength=2}
if ql in range(2,7):{
wordlength=3}
if ql in range(8,33):{
wordlength=4}
if ql in range(34,85):{
wordlength=5}
if ql in range(86,170):{
wordlength=6}
if ql in range(171,292):{
wordlength=7}
if ql in range(293,432):{
wordlength=8}
if ql in range(433,572):{
wordlength=9}
if ql in range(573,698):{
wordlength=10 }
if ql in range(699,799):{
wordlength=11}
if ql in range(800,874):{
wordlength=12}
if ql in range(875,926):{
wordlength=13}
if ql in range(927,958):{
wordlength=14}
if ql in range(959,978):{
wordlength=15}
if ql in range(979,988):{
wordlength=16}
if ql in range(989,994):{
wordlength=17}
if ql in range(995,997):{
wordlength=18}
if ql in range(998,999):{
wordlength=19}
if ql==1000:{
wordlength=20}
}
else:{
wordlength=int(wordlength)
}
while counter<wordlength:{
word=word+alphabet[random.randint(0,25)]
if len(word)==wordlength:{
if isValid(word)==True:{
print("The generated word is: "+word)}
else:{
word=""
counter=0}
}
counter=counter+1
}
I haven't put all those brackets {} in the real file but I saw my indents didn't make it through to the layout on the threadpage, so I've added those to clarify.
On request, I can post the file I'm using with all the English words, but that might take up some space here, and I'm absolutely sure the file is correct (my teacher provided me with it in an earlier assignment and it worked perfectly). For the layout though, here are the first 10 lines:
a
aah
aahed
aahing
aahs
aardvark
aardvarks
aardwolf
ab
abaci
(You guys understand, it's really ALL English words ;))