Search code examples
pythonstringuser-interfaceeasygui

Compare user input to known string


I'm trying to compare a word provided by the user (e.g "orange") to a list of words I have in a text file like the following:

menu.txt

apple 
banana
orange
grape
mango

The user input comes from an easygui.enterbox. I'm never getting the result I expect because it is having a hard time comparing the strings. Here is my code.

import easygui     

count = 0
dish = easygui.enterbox("enter your favourite dish:")
with open("menu.txt") as f:
    content = f.readlines()
    for item1 in content:
        if item1 == dish :
            easygui.msgbox("order taken.thankyou")
            count = count + 1
            continue
        if count == 0 :
            easygui.msgbox("plz order some other item")

Solution

  • It might be that you need to add .strip() to both item and dish to make sure all spaces or end of line symbols are not part of the string