我无法理解为什么此代码返回类型错误
if response == "1":
return print(" You've selected the Bundle Package! Please schedule a home visit and our technician will come and set up your new service.") + home_visit("new install")
TypeError:+的不支持的操作数类型:'NoneType'和'str
是因为我试图调用函数以及打印语句吗?
这可能是你的错误观念。返回print()不会返回包含它的字符串。如果你想仍然在里面打印字符串,然后调用函数,并将打印的字符串返回到你调用它的任何地方,你可以这样做:
if response == "1":
# this is the string you want
whatIWantToPrint = " You've selected the Bundle Package! Please schedule a home visit and our technician will come and set up your new service."
# this is where you print it
print(whatIWantToPrint)
# this is where you call the function
home_visit("new install")
# this is where you return the string you printed
return whatIWantToPrint
这是因为打印不返回任何内容。它只是打印。如果你想返回print + home_visit那么:
return f"You've selected the Bundle Package! Please schedule a home visit and our technician will come and set up your new service.{home_visit('new install')}"
该错误表示您正在尝试连接打印的返回,即NoneType和字符串。你应该打印然后返回字符串。这是一个例子:
if response == "1":
print(" You've selected the Bundle Package! Please schedule a home visit and our technician will come and set up your new service.")
return home_visit("new install")