繁体   English   中英

我收到类型错误,但不确定为什么吗?

[英]I'm getting a type error but not sure why?

我不知道为什么此代码返回类型错误

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 + 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')}"

这可能是您的误解。 返回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

该错误表明您正在尝试合并返回的输出,即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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM