我收到类型错误,但不知道为什么?

问题描述 投票:0回答:3

我无法理解为什么此代码返回类型错误

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

是因为我试图调用函数以及打印语句吗?

python python-3.x
3个回答
1
投票

这可能是你的错误观念。返回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

2
投票

这是因为打印不返回任何内容。它只是打印。如果你想返回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')}"

0
投票

该错误表示您正在尝试连接打印的返回,即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")
© www.soinside.com 2019 - 2024. All rights reserved.