返回“Hello”+ Input +“!”的Python函数除两个具体案件外

时间:2016-12-20 00:56:27

标签: python string function if-statement

我正在尝试创建一个Python函数,为除Lewis和Clark之外的所有人返回一个礼貌的问候语。这是我试过的:

import os
from timeit import default_timer as timer

features_to_delete = ['a','b','c']
start = timer()
for x in features_to_delete:

    name_checker = str(x) + '.jpg'
    print 'this is name checker {}'.format(name_checker)
    folder = '.'
    for root, dir2, files in os.walk(folder):
        print 'This is the root directory at the moment:{} The following are files inside of it'.format(root)

        for b in files:
            if b.endswith('.jpg'):
                local_folder = os.path.join(folder, root)
                print 'Here is name of file {}'.format(b)
                print 'Here is name of name checker {}'.format(name_checker)
                counter = 0
                if b == name_checker:
                    counter += 1
                    print '{} needs to be deleted..'.format(b)
                    os.remove(os.path.join(local_folder, b))
                    print 'Removed {} \n'.format(os.path.join(local_folder, b))

                else:
                    print 'This file can stay {} \n'.format(b)
            else:
                pass

end = timer()
print(end - start)

start = timer()
features_to_delete = ['d','e','f']
matches = []
folder = '.'
for x in features_to_delete:
    x = str(x) + '.jpg'
features_to_delete = [e + '.jpg' for e in features_to_delete]
print 'features' + str(features_to_delete)
for root, dirnames, filenames in os.walk(folder):
    for filename in set(filenames).intersection(features_to_delete):#fnmatch.filter(filenames, features_to_delete)# fnmatch.filter(filenames, features_to_delete):
        local_folder = os.path.join(folder, root)
        os.remove(os.path.join(local_folder, filename))
        print 'Removed {} \n'.format(os.path.join(local_folder, filename))
end = timer()
print(end - start)

现在,这是为每个人打印$ touch foo/bar/d.jpg $ touch foo/bar/b.jpg $ python deletefiles.py this is name checker a.jpg This is the root directory at the moment:. The following are files inside of it This is the root directory at the moment:./.idea The following are files inside of it This is the root directory at the moment:./foo The following are files inside of it This is the root directory at the moment:./foo/bar The following are files inside of it Here is name of file d.jpg Here is name of name checker a.jpg This file can stay d.jpg Here is name of file b.jpg Here is name of name checker a.jpg This file can stay b.jpg this is name checker b.jpg This is the root directory at the moment:. The following are files inside of it This is the root directory at the moment:./.idea The following are files inside of it This is the root directory at the moment:./foo The following are files inside of it This is the root directory at the moment:./foo/bar The following are files inside of it Here is name of file d.jpg Here is name of name checker b.jpg This file can stay d.jpg Here is name of file b.jpg Here is name of name checker b.jpg b.jpg needs to be deleted.. Removed ././foo/bar/b.jpg this is name checker c.jpg This is the root directory at the moment:. The following are files inside of it This is the root directory at the moment:./.idea The following are files inside of it This is the root directory at the moment:./foo The following are files inside of it This is the root directory at the moment:./foo/bar The following are files inside of it Here is name of file d.jpg Here is name of name checker c.jpg This file can stay d.jpg 0.000916957855225 features['d.jpg', 'e.jpg', 'f.jpg'] Removed ././foo/bar/d.jpg 0.000241994857788 ,而不仅仅是刘易斯和克拉克。我不明白为什么 - 我的IDE没有返回任何错误消息。如果可以,请帮忙,谢谢。

3 个答案:

答案 0 :(得分:4)

这应该可以解决您在每次比较中不重复条件==的问题。 LewisClark

def politeGreeting(name):
  #if the user's name is Lewis or Clark, say "Oh, it's you." 
  if name == "Lewis" or name == "Clark":
    return("Oh, it's you")
  #if the user's name is anything else
  else:
    return("Hello, " + name + "!")

name = input("Please enter your name:")        
print(politeGreeting(name))

但是,如果您想允许不同的输入大写,请尝试使用str.lower()str.title()的此类内容:

def politeGreeting(name):
  #if the user's name is Lewis or Clark, say "Oh, it's you." 
  if name.lower() in {"lewis", "clark"}: # Use set for O(1) lookup time
    return("Oh, it's you " + name.title())
  #if the user's name is anything else
  else:
    return("Hello, " + name.title() + "!")

name = input("Please enter your name:")        
print(politeGreeting(name))

答案 1 :(得分:0)

表达式name == "Lewis" or "Clarke"将始终评估为True。请改用name in ("Lewis", "Clarke")

def politeGreeting(name):
    #if the user's name is Lewis or Clark, say "Oh, it's you." 
    if name in ("Lewis", "Clark"):
        return("Oh, it's you")
    #if the user's name is anything else
    else:
        return("Hello," + name + "!")

    name = input("please enter your name")        
    print (politeGreeting(name))

答案 2 :(得分:0)

def politeGreeting(name):
    #if the user's name is Lewis or Clark, say "Oh, it's you." 
    if if name == "Lewis" or name == "Clark":
        return("Oh, it's you")
    #if the user's name is anything else
    else:
        return("Hello," + name + "!")

name = input("please enter your name")        
print (politeGreeting(name))