我正在尝试为小型硬件作业创建一个madlib。尝试修复第15行中的错误。
我尝试使用“ str(None)”代替计划“''”
import random
print('Time for M A D L I B S')
print('Enter examples of some zany words for each category!! Use quote marks')
random_name = input("Enter random name:")
your_name = input('Enter your own name:')
place = input('Enter a place:')
adjective = input('Enter an adjective:')
adjs = ['crazy', 'nice', 'awesome', 'big','tiny']
verbs = ['met', 'ran', 'farted', 'sat on', 'hugged']
prepositions = ['above the', 'near the', 'around the', 'behind', 'beside']
print(random.choice(adjs)) + ' ' + random_name + ' ' + print(random.choice(verbs)) + ' ' + your_name + ' ' + print(random.chioce(prepositions)) + ' ' + adjective + ' ' + place
Traceback (most recent call last):
File "madlibs.py", line 15, in <module>
print(random.choice(adjs)) + ' ' + random_name + ' ' + print(random.choice(verbs)) + ' ' + your_name + ' ' + print(random.chioce(prepositions)) + ' ' + adjective + ' ' + place
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
答案 0 :(得分:2)
第15行:
print(random.choice(adjs)) + ' ' + random_name + ' ' + print(random.choice(verbs)) + ' ' + your_name + ' ' + print(random.chioce(prepositions)) + ' ' + adjective + ' ' + place
您正在串联print(random.choice(verbs))
,并返回NoneType
和str
类型。尝试删除多余的打印语句,使其看起来像这样:
print(random.choice(adjs)) + ' ' + random_name + ' ' + random.choice(verbs) + ' ' + your_name + ' ' + random.chioce(prepositions) + ' ' + adjective + ' ' + place)