【问题标题】:Type Error: unsupported operand type(s) for +: 'NoneType' and 'str' exit status 1类型错误:+ 不支持的操作数类型:“NoneType”和“str”退出状态 1
【发布时间】:2020-11-13 00:05:53
【问题描述】:

我一直在编辑我的文本,但我一直收到同样的错误!

我的代码:

import random

Female_Characters = ["Emily", "Ariel", "Jade", "Summer"]
Male_Characters = ["Blake", "Max", "Jack", "Cole", "Daniel"]
PlacesToMeet = ["Beach", "Park", "Train Station", "Cave"]
SheSaid = ["Lets go explore!", "I'm tired", "I saw a purple frog", "My tounge hurts"]
HeSaid = ["I didnt get much sleep", "I wanna go inside that cave!", "Oh, ok"]
Outcomes = ["They never got to", "They were never found again.", "They enjoyed their day and went to 
get Ice Cream!"]

ChosenFemale = random.choice(Female_Characters)
ChosenMale = random.choice(Male_Characters)
ChosenMeet = random.choice(PlacesToMeet)
ChosenShesaid = random.choice(SheSaid)
ChosenHeSaid = random.choice(HeSaid)

print ("There were two friends, their names are ") + (ChosenMale) + (", and ") + (ChosenFemale) + (".") + ("One day when they were at the") + (ChosenMeet) + (", ") + (ChosenFemale) + (" Said, ") + (ChosenShesaid) + (". Then") + (ChosenMale) + (" Said ") + (ChosenHeSaid) + (". After that, ") + random.choice(Outcomes)

python 还是新手

【问题讨论】:

标签: python


【解决方案1】:

print 行中的括号不正确。

print("There were two friends, their names are " + ChosenMale + ", and " + ChosenFemale + "." + "One day when they were at the" + ChosenMeet + ", " + ChosenFemale + " Said, " + ChosenShesaid + ". Then" + ChosenMale + " Said " + ChosenHeSaid + ". After that, " + random.choiceOutcomes)

您的代码正在调用

print("There were two friends, their names are ")

它返回None,然后尝试将其与所有其他字符串连接起来。

也许您正在遵循 Python 2.x 的说明。在 Python 2 中,print 是一个语句,因此它不带括号中的参数,但在 Python 3 中,它是一个普通函数。

【讨论】:

    【解决方案2】:

    去掉这个字符串末尾的括号

    print("There were two friends, their names are "
    

    并在末尾添加

    random.choice(Outcomes)
    

    应该是这样的:

    print ("There were two friends, their names are " + (ChosenMale) + (", and ") + (ChosenFemale) + (".") + ("One day when they were at the") + (ChosenMeet) + (", ") + (ChosenFemale) + (" Said, ") + (ChosenShesaid) + (". Then") + (ChosenMale) + (" Said ") + (ChosenHeSaid) + (". After that, ") + random.choice(Outcomes))
    

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 2018-07-07
      • 2023-01-04
      • 2023-01-24
      • 2017-07-11
      • 2019-08-22
      • 2015-09-21
      • 2017-05-27
      相关资源
      最近更新 更多