我正在制作一个伪单词(假的,发音真实的单词)生成器,它按预期工作,但是后来我决定制作它,以便它一次可以生成多个单词,(您将其赋予“ n”字数为n)。因此,我将所有内容放入for循环中...并且可以正常工作...但是说,例如,您要求输入6个单词,它会给您6次相同的单词,而不是每次都生成一个新单词。
import random
wordend = "false"
word = ""
sniptypes = ["v","c"]
vowels = ["a","e","i","o","u"]
vends = ["a","e","y","o","u"]
#vends is short for vowel ends (for when a word ends with a vowel)
cends = ["b","c","d","f","k","l","m","h","n","p","g","q","s","r","t","v","w","x","y","z"]
#cends is short for consonant ends (these are the consonants the words are allowed to end with)
dends = ["kt","rt","ld","rc","rk","fk","lm","by","ch","ck","cy","dy","fy","gh","gy","ky","ly","quy","my","ny","nd","pt","ph","py","ry","rg","rf","rd","xy","sb","sh","sk","sp","st","sy","th","ty","wy","ss","tt","ll","nm","mn"]
#dends is short for double ends (pair of consonants that sound well at the end of a word: chalCK)
consonants = ["b","c","d","f","g","h","j","k","l","m","n","p","qu","r","s","t","v","w","x","y","z"]
doubles = ["bl","br","ch","ck","cl","cr","dl","dr","fl","fn","fr","fy","gh","gl","gr","gn","vy","kl","kn","kr","xy","zy","ly","pn","pt","ph","pl","pr","py","sb","sc","sh","sk","sl","sn","sm","sp","squ","st","sv","sy","th","tl","tr","ty","ts","vl","wh","wr","ll","tt","ss"]
#doubles are a pair of consonants that sound well at the beggining of a word: CHalk.
lengths = [2,3,3,4,4,4,4,5,5,6]
for i in range(int(input("how many words? "))):
lenght = random.choice(lengths)
sniptype = random.choice(sniptypes)
snipnumb = lenght
while wordend == "false":
snipnumb -= 1
if sniptype == "v":
r1 = random.random()
if r1 < (5/21):
r2 = random.random()
if r2 < (1/5):
word += random.choice(vowels)
word += random.choice(vowels)
if snipnumb == 0:
word += random.choice(vends)
elif snipnumb > 0:
word += random.choice(vowels)
else:
print("error")
elif r2 >= (1/5):
word += random.choice(vowels)
if snipnumb == 0:
word += random.choice(vends)
elif snipnumb > 0:
word += random.choice(vowels)
else:
print("error")
else:
print("error")
elif r1 >= (5/21):
if snipnumb == 0:
word += random.choice(vends)
elif snipnumb > 0:
word += random.choice(vowels)
else:
print("error")
else:
print("error")
sniptype = "c"
elif sniptype == "c":
r1 = random.random()
if r1 < (6/21):
r2 = random.random()
if r2 < (1/6):
if snipnumb == 0:
word += random.choice(dends)
elif snipnumb > 0:
word += random.choice(consonants)
word += random.choice(doubles)
else:
print("error")
elif r2 >= (1/6):
if snipnumb == 0:
word += random.choice(dends)
elif snipnumb == (lenght-1):
word += random.choice(doubles)
elif snipnumb > 0:
word += random.choice(consonants)
word += random.choice(consonants)
else:
print("error")
else:
print("error")
elif r1 >= (6/21):
if snipnumb == 0:
word += random.choice(cends)
elif snipnumb > 0:
word += random.choice(consonants)
else:
print("error")
else:
print("error")
sniptype = "v"
else:
print("error")
if snipnumb == 0:
wordend = "true"
print(word)
我希望它能生成n个不同的单词,相反,它会生成一个单词并将其打印n次。
最佳答案
将生成随机单词的代码用作函数,然后重复调用该函数。真正相关的更改在底部,但是在所有代码中都进行了一些更改。
import random
def get_random_word():
wordend = "false"
word = ""
sniptypes = ["v","c"]
vowels = ["a","e","i","o","u"]
vends = ["a","e","y","o","u"]
#vends is short for vowel ends (for when a word ends with a vowel)
cends = ["b","c","d","f","k","l","m","h","n","p","g","q","s","r","t","v","w","x","y","z"]
#cends is short for consonant ends (these are the consonants the words are allowed to end with)
dends = ["kt","rt","ld","rc","rk","fk","lm","by","ch","ck","cy","dy","fy","gh","gy","ky","ly","quy","my","ny","nd","pt","ph","py","ry","rg","rf","rd","xy","sb","sh","sk","sp","st","sy","th","ty","wy","ss","tt","ll","nm","mn"]
#dends is short for double ends (pair of consonants that sound well at the end of a word: chalCK)
consonants = ["b","c","d","f","g","h","j","k","l","m","n","p","qu","r","s","t","v","w","x","y","z"]
doubles = ["bl","br","ch","ck","cl","cr","dl","dr","fl","fn","fr","fy","gh","gl","gr","gn","vy","kl","kn","kr","xy","zy","ly","pn","pt","ph","pl","pr","py","sb","sc","sh","sk","sl","sn","sm","sp","squ","st","sv","sy","th","tl","tr","ty","ts","vl","wh","wr","ll","tt","ss"]
#doubles are a pair of consonants that sound well at the beggining of a word: CHalk.
lengths = [2,3,3,4,4,4,4,5,5,6]
lenght = random.choice(lengths)
sniptype = random.choice(sniptypes)
snipnumb = lenght
while wordend == "false":
snipnumb -= 1
if sniptype == "v":
r1 = random.random()
if r1 < (5/21):
r2 = random.random()
if r2 < (1/5):
word += random.choice(vowels)
word += random.choice(vowels)
if snipnumb == 0:
word += random.choice(vends)
elif snipnumb > 0:
word += random.choice(vowels)
else:
print("error")
elif r2 >= (1/5):
word += random.choice(vowels)
if snipnumb == 0:
word += random.choice(vends)
elif snipnumb > 0:
word += random.choice(vowels)
else:
print("error")
else:
print("error")
elif r1 >= (5/21):
if snipnumb == 0:
word += random.choice(vends)
elif snipnumb > 0:
word += random.choice(vowels)
else:
print("error")
else:
print("error")
sniptype = "c"
elif sniptype == "c":
r1 = random.random()
if r1 < (6/21):
r2 = random.random()
if r2 < (1/6):
if snipnumb == 0:
word += random.choice(dends)
elif snipnumb > 0:
word += random.choice(consonants)
word += random.choice(doubles)
else:
print("error")
elif r2 >= (1/6):
if snipnumb == 0:
word += random.choice(dends)
elif snipnumb == (lenght-1):
word += random.choice(doubles)
elif snipnumb > 0:
word += random.choice(consonants)
word += random.choice(consonants)
else:
print("error")
else:
print("error")
elif r1 >= (6/21):
if snipnumb == 0:
word += random.choice(cends)
elif snipnumb > 0:
word += random.choice(consonants)
else:
print("error")
else:
print("error")
sniptype = "v"
else:
print("error")
if snipnumb == 0:
wordend = "true"
return word
word_count = int(input("how many words? "))
counter = 0;
while(counter < word_count):
print(get_random_word())
counter+=1