简体   繁体   中英

python character in string checking and printing

This is a noob question asked by a noob. I need to create a function to check whether or no all the character in the string can be found in 'scraps', if true, just print out the whole string once, else, print the error message below. I am sure the real solution is pretty simple but at the moment I feel like I am solving calculus problem with grade school math. please advise. thanks~

def fix_it(scraps, recycled):

The function should be able to produce:

print fix_it('AbCdEfG', 'AhK') ==> "Give me something that's not useless next time."

print fix_it('AbCdEfG', 'CdE') ==> 'CdE'

How about this:

def fix_it(scraps, recycled):
    for i in recycled:
        if i not in scraps:
            return "give me something useful"
    return recycled

print fix_it('AbCdEfG', 'AhK')
print fix_it('AbCdEfG', 'CdE')

output is:

python test.py
give me something useful
CdE

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