类型错误:不支持+:“非类型”和“str”退出状态1的操作数类型

2024-10-02 04:25:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在编辑我的文字,但我不断得到相同的错误

我的代码:

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还是新手


Tags: gogetthatrandommalefemalechoicethey
2条回答

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,然后尝试将其与所有其他字符串连接起来

也许您在遵循Python2.x的说明。在Python2中,print是一个语句,因此它没有在括号中包含参数,但在Python3中它是一个普通函数

删除此字符串末尾的括号

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))

相关问题 更多 >

    热门问题