【发布时间】:2013-12-25 09:33:03
【问题描述】:
我一直在尝试在 Python 3.3.3 中编写一个 Mad-Libs 模拟器,但一直收到错误消息:
Traceback (most recent call last):
File "/Users/RuzHayes_Laptop/Desktop/Programming:Design/Programs/Python Mad Libs Program 000.py", line 80, in <module>
templates=[("The"+" "+adj+" "+n+" "+v+" "+adv+pun),(adj+" "+pluralize(n)+' '+(v[:len(v)-1])+" "+adv+pun)]
TypeError: Can't convert 'NoneType' object to str implicitly
在以下代码中:
print("Welcome!")
print("When you would like to try out ")
print("Python Mad Libs Program, Prototype000,")
begin=input("press the enter/return key.")
print()
print()
print("Initializing befuddlement...")
print()
import random
sentenceCap=35
sentenceBottom=25
numOfSentences=random.randint(sentenceBottom,sentenceCap)
caps={"a":"A","b":"B","c":"C","d":"D",'e':'E','f':'F','g':'G','h':'H','i':'I','j':'J','k':'K','l':'L','m':'M','n':'N','o':'O','p':'P','q':'Q','r':'R','s':'S','t':'T','u':'U','v':'V','w':'W','x':'X','y':'Y','z':'Z'}
tempstore=[" "]*numOfSentences
irregplrls={'child':'children','ox':'oxen','moose':'moose'}
def testforoo(x):
for j in range(0,len(x)):
if j+1<=len(x) and x[j:j+1]=='oo'.lower():
return True
return False
def pluralize(x):
l=len(x)
for i in irregplrls:
if i == x:
return irregplrls[x]
if x[l-1]=="y":
return x[:l-1]+"ies"
elif x[l-1]=="s" and x[l-2]=="u":
return x[:l-2]+"i"
elif x[l-1] in ('s','x'):
return x+"es"
elif x[-2:] in ('ch','sh'):
return x+"es"
elif 'f'==x[l-1] or x[l-2]:
if 'f'==x[l-1]:
return x[:l-1] + 'ves'
elif 'f'==x[l-2]:
return x[:l-2]+"ves"
elif testforoo(x)!=False:
return x[:testforoo(x)-2]+'ee'+x[testforoo(x):]
else:
return x+'s'
print()
print("Retrieving craploads of words...")
print()
verb=["moves","jumps", "hides","sniffs","gazes","sneezes","calls"]
noun=["rabbit","dog","cat","otter","seal","elephant","fox",'baby','moose','octopus']
adjec=["happy","angry","cute","enormous","elegant","annoying"]
adver=["merrily","frustratedly","incoherently","morosely","peppily",'exuberantly']
endpunct=[".","!"]
print()
print("Simulating human grammar-speak...")
print()
print()
for i000 in range(0,numOfSentences):
v=random.choice(verb)
n=random.choice(noun)
adj=random.choice(adjec)
adv=random.choice(adver)
pun=random.choice(endpunct)
askinput=random.randint(0,round(numOfSentences/5))
whichinput=random.randint(0,3)
if askinput==0:
if whichinput==0:
n=input("Please input a noun. ")
elif whichinput==1:
v=input("Please input a verb. ")
elif whichinput==2:
adj=input("Please input an adjective. ")
elif whichinput==3:
adv=input("Please input an adverb. ")
templates=[("The"+" "+adj+" "+n+" "+v+" "+adv+pun),(adj+" "+pluralize(n)+' '+(v[:len(v)-1])+" "+adv+pun)]
final=templates[random.randint(0,len(templates)-1)]
if final[:1]==final[:1].lower():
final=caps[final[:1]]+final[1:]
tempstore[i000]=final
print()
print()
print("Producing proof of illiteracy...")
print()
print()
for i001 in range(0,len(tempstore)):
sent=tempstore[i001]
print(sent)
我很困惑,我需要帮助。我现在发现问题出在复数定义的末尾,但除此之外我很困惑。该程序一直有效,直到我更改了复数以解释某些名词没有正确复数。
如果您能给我任何帮助,我将不胜感激。我现在只编程了大约一个月,这是我尝试过的最难的程序。
谢谢你,圣诞快乐!当然,如果你庆祝圣诞节的话。
【问题讨论】:
-
显然是您尝试插入到
None中的字符串中的对象之一。 -
for i000 in...和for i0001...天哪,为什么是这些名字?
标签: python python-3.x