简体   繁体   中英

I am trying to see if any value of one list matches any value of the other list, But I can't make it this way. Please help!..I don't understand why

I am trynna make a code that checks whether a string value from a list is similar to other string value in another list..But this doesn't work, I am sure that the problem lies in the loop statement..But I can't figure it out..

import random as r

explc_words = ['f***','sh*t','dumb']

breakdown_list = ['Hello','myself','Rahul','sh*t','I','messed','up']

original = 'Hello, myself Rahul, Sh*t I messed up'

mindlang_words =['Please check again','Was it intentional?','please mind your language']

for j in explc_words:

    if j in breakdown_listi:

        print(r.choice(mindlang_words))
        break
    else:
        print("Please recheck everything you typed:", original)
        break

Are you aware you have a typo? the variable "breakdown_listi" is not defined. You probably meant "breakdown_list".

After correcting this, I ran your code and got this:

Please recheck everything you typed: Hello, myself Rahul, Sh*t I messed up

I think the code you probably want is something like this:

if any(xw in breakdown_list for xw in explc_words):
    print(r.choice(midlang_words))
else:
    print("Please recheck everything you typed", original)

The issue is that for either condition you break the loop, so only the first word is getting tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM