简体   繁体   English

字符串检查和打印中的python字符

[英]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.我需要创建一个函数来检查字符串中的所有字符是否都可以在 'scraps' 中找到,如果为真,只需打印一次整个字符串,否则,打印下面的错误消息。 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): def fix_it(废料,回收):

The function should be able to produce:该功能应该能够产生:

print fix_it('AbCdEfG', 'AhK') ==> "Give me something that's not useless next time."打印 fix_it('AbCdEfG', 'AhK') ==> “下次给我一些没用的东西。”

print fix_it('AbCdEfG', 'CdE') ==> 'CdE'打印 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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