import easygui
flavour = easygui.enterbox('What is your favourite ice cream flavour?')
easygui.msgbox ('You entered ' + flavour)
我在这里做什么,以便在我点击“取消”时按钮上输入'它不会返回错误?目前我收到以下错误: " easygui.msgbox('你进入' +风味) TypeError:必须是str,而不是NoneType"
答案 0 :(得分:1)
这样做
import easygui
while True:
flavour = easygui.enterbox('What is your favourite ice cream flavour?(type quit to quit)')
a = bool(flavour)
if a == False:
easygui.msgbox('you did not enter something')
elif a == True:
if flavour == 'quit':
break
else:
easygui.msgbox ('You entered ' + flavour)
(我可以让程序更好但更大更大)
答案 1 :(得分:0)
发生的事情是msgbox
希望消息是字符串。但是,如果您点击取消,flavour
是NoneType
对象。您可以添加if语句,以确保在单击取消时代码不会出错。做类似的事情:
flavour = easygui.enterbox('What is your favourite ice cream flavour?')
if flavour is not None:
easygui.msgbox ('You entered ' + str(flavour))
else:
pass