0

Why does this program return true when the words 'this', 'that', and 'and' are not actually a part of the string? (Python 3.7)

I came across this issue while trying to solve Project Euler problem 59. I was iterating over the possible keys and checking if the resulting deciphered string contained common words. Doing so, I ended up with an erroneous key as the resulting string returns true for the given conditions.

res='Et!abuv{bp:ueqdj:gvul$nia:hjnsk~tgnhkt!k|!ktd$ug$_ths#i!iurp:bavdfh`pe$j`tsw6!&^d$itiwhw:rahhahti:sayhthng{sqw#$ANj:ul!wolw:nb:rahhai!k|!vbmjsky`hi\>:H$r`r!vbatuhc!butj~-$ktmnd$ooabqayua~m}6!et!avdc{op:d|jsairmuo$|nv:ul!atumhd$iti:nb:ulsr$idvsdw:0$1!555$1!558$1!5502:*$ug4-$mimyi$~dto`i!kt!prd$kte~sentv!k|!prd$yhvyma6!wu!pr`p:hb:ul!phta:rqw!k|!prhw:rahhai!mi!kxuesoa~-$|skw!mn!en!ktba:ul!uo``h`posa:nb:ul!gssgvd$|nhvnsi/$T`im}6!M:ield$|nqte$nien!prd$iti:nb:ulsr$idvsdw:hw:`$ih|ni$j`vn!k|!prd$ipq{sa:nb:ul!tsmwdps$ug$nia:bmhbh!srnw!`s`iuah!mi!5!!kh!fc!toupsoc:ul!wol$ug$nimi!wsmr$pq{m$nn$i-$su$r`w:ul!v{umu!wksp27-:lqvumjmme$xx$i!pu!5:nb:ul!tsmwdps$nn$nia:em{landv4!M:vmvm$inkt!wrns:ul{u$nia:rqw!k|!prhw:rahhai!pu!f!ejqvuymw`pm}:0*,50#20*72"56(70)70!!ete$|skw!iompsqhchj}!prhw:oqwcah!fc!wsy(:`j~!prdj:ueqhj}!prd$ipq{sa:skuu(:ul!jolfs$)/5.01#32/21"83#26)9$sr$so`d`:qvueqyd`6!srhgr!abqvrwr$nia:qahhiuah!k|!e:bmhbh!srnw!`s`iuah!mi!54!Bumhuvmtf${feso$nia:rewd$iuajr$xx$mimyi$S!l{e${svswa~!en!prhw:rqw-$S!l{wa:emibkldve$nien!prd$iti:nb:ul!wsmr$+!/:0++7$1!5595:*$+.6/7$1!5576/!/:dpy/${mwu!`qatew:nj:ul!uo``h`posa:nb:ul!gssgvd*:Oewdhc-$nia:rqw!k|!prhw:lqvumjmme$xx$#1$}hrr$nia:cmkte~send$2gkospr!tuvah($ug$nia:bmhbqwgahdjyd$ug$nia:qahhiuah!k|!e:bmhbh!srnw!`s`iuah!mi!54!Ete$xx$ihismeh!v`wuomtf$S!l{wa:mmqdssra:cao${ch!pu!`uahlmtd$nia:rqwr$ug$nia:rqxraktatu$idvsdw:hj:vlsbl:ul!abqktdjnr${sa:dro$ttixdvi/'

print('this' and 'that' and 'and' in res)

Kindly excuse me if this is a basic question, but I've been left scratching my head to figure out why this is happening.

2 Answers2

1

You are not checking the condition right. You should rather

print('this' in res and 'that' in res and 'and' in res)

What you are checking is actually "true" in Python. Refer to this example below to find out

if 'this':
    print('found') # will print found
mad_
  • 8,121
  • 2
  • 25
  • 40
0

if you want to check if ANY of them are in res:

print('this' in res or 'that' in res or 'and' in res)

if you want to check if ALL of them are in res:

print('this' in res and 'that' in res and 'and' in res)

if you want multiple: words = [list of words you want] for word in words: if word in original_string: if(word in original_string): print('{} is in {}'.format(word,original_string))

basically, you have you list of words, and loop through them, and for each one that the original string contains, it will tell you which word and what the original string is.

mark pedersen
  • 245
  • 1
  • 9