Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

python 3.x - why returns does not match with string?

my Setting.txt:
mode = spam

def read_setting():
    try :
        setting = open("Setting.txt","r")
        mode = setting.readline().split(" ")[2]
        print("Mode: "+mode)
        return str(mode)
    except:
        print("There is/are error in Setting.txt
Please check if you have the file / correct the file formatting")
        exit()

The func returns spam(confirmed this by manual debugging and its returns what i want) ,
But at my main program ,

def intro():
    print("This is just a print
")

def main():
    intro()
    moded = read_setting()
    if moded == "spam":
        timer()
    elif moded == "paste":
        paste()

if __name__ == '__main__':
    main()


the func timer() does not run
it turns out, when i do print(moded=="spam") , it returns False
What am i wrong here?
My Python:

Python 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] on linux

question from:https://stackoverflow.com/questions/65856858/why-returns-does-not-match-with-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can see the differences if you do print(ascii(moded))
It will gives an output of spam
that's why spam == spam returns False
do a moded = moded.rstrip(" ") and that'll solved the problem


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...