easygui.msgbox ('你输入了 ' + flavor) TypeError: must be str, not NoneType [英] easygui.msgbox ('You entered ' + flavour) TypeError: must be str, not NoneType
问题描述
import easygui
flavour = easygui.enterbox('What is your favourite ice cream flavour?')
easygui.msgbox ('You entered ' + flavour)
我该怎么做才能在我点击输入"框中的取消"按钮时不返回错误?目前我收到以下错误:"easygui.msgbox('你输入了'+风味)类型错误:必须是 str,而不是 NoneType"
What do I do here so that when I hit the 'cancel' button on the 'enter' box it doesn't return an error? At the moment I get the following error: "easygui.msgbox ('You entered ' + flavour) TypeError: must be str, not NoneType"
推荐答案
发生的事情是 msgbox
希望消息是字符串.然而,如果你点击取消,flavour
是一个 NoneType
对象.您可以添加 if 语句以确保您的代码在您点击取消时不会出错.执行以下操作:
What is happening is that msgbox
wants the message to be string. However, if you hit cancel, flavour
is a NoneType
object. You can add an if statement to make sure your code does not error out if you hit cancel. Do something like:
flavour = easygui.enterbox('What is your favourite ice cream flavour?')
if flavour is not None:
easygui.msgbox ('You entered ' + str(flavour))
else:
pass
这篇关于easygui.msgbox ('你输入了 ' + flavor) TypeError: must be str, not NoneType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!