python-3.6easygui

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)

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"


Solution

  • 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