python - Strings won't compare properly -
i'm new python excuse poor code may find
the problem i'm having i'm trying compare 2 strings, 1 file , 1 user input, see if match. however, when comparing seem me identical strings, if statement returns false. have used str()
function turn both of values strings, still doesn't want play ball.
for reference, line of file 2 strings, separated comma, , 'question' part before comma , 'answer' after it. @ present, both strings test strings, , have values of 'question' , 'answer'
import linecache def askquestion(question, answer): choice = input(question) str(choice) if choice == answer: print("correct") else: print("bad luck") file = "c://questions.txt" text = linecache.getline(file, 1) question = text.split(",", 1)[0] answer = text.split(",", 1)[-1] str(answer) askquestion(question, answer)
i typing in file has i.e. capital letters in right place, note. i'm sure answer obvious, i'm @ loss, kindly appreciated.
thanks!
text = linecache.getline(file, 1) question = text.split(",", 1)[0] answer = text.split(",", 1)[-1]
this more commonly written
text = linecache.getline(file, 1) question, answer = text.split(",", 1)
the results string. there no interpretation happening. also, need store result of str()
.
my_string = str(number)
by calling str()
not saving result being lost. not important here, keep in mind.
as alex mentions in comment above missing call strip
remove newline. how know? tried in interpreter.
$ python >>> import linecache >>> line = linecache.getline("tmpfile", 1) >>> line
'* contact numbers\n'
see '\n' there? problem.
another way have been invoke pdb
python debugger. add
import pdb; pdb.set_trace()
where want stop execution of python script.
good luck.
Comments
Post a Comment