如何解决:TypeError:+不支持的操作数类型:“ NoneType”和“ str”

时间:2019-06-17 00:35:59

标签: python string formatting

我正在尝试为小型硬件作业创建一个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'

1 个答案:

答案 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)),并返回NoneTypestr类型。尝试删除多余的打印语句,使其看起来像这样:

print(random.choice(adjs)) + ' ' + random_name + ' ' + random.choice(verbs) + ' ' + your_name + ' ' + random.chioce(prepositions) + ' ' + adjective + ' ' + place)