sleepylog
sleepylog

Reputation: 67

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"

Upvotes: 0

Views: 508

Answers (2)

Haoyang Song
Haoyang Song

Reputation: 162

this will do it

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)

(i can make the program better but bigger and larger)

Upvotes: 1

MattR
MattR

Reputation: 5126

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

Upvotes: 0

Related Questions