如何在使用PYTHON的函数中使用条件(多个IF和ELSE语句)

如何在使用PYTHON的函数中使用条件(多个IF和ELSE语句),python,function,conditional,Python,Function,Conditional,我是编程新手,正在edx.org上学习一门课程 我在函数中使用条件时遇到问题。每次调用该函数时,它都会给出所需的输出,但最后也会显示“无”。有没有办法在代码中使用return关键字?下面是问题和我的代码 ###create a functions using startswith('w') ###w_start_test() tests if starts with "w" # function should have a parameter for test_string and

我是编程新手,正在edx.org上学习一门课程

我在函数中使用条件时遇到问题。每次调用该函数时,它都会给出所需的输出,但最后也会显示“无”。有没有办法在代码中使用return关键字?下面是问题和我的代码

###create a functions using startswith('w')    

###w_start_test() tests if starts with "w"

 # function should have a parameter for test_string and print the test result

 # test_string_1 = "welcome"
 # test_string_2 = "I have $3"
 # test_string_3 = "With a function it's efficient to repeat code"

# [ ] create a function w_start_test() use if & else to test with startswith('w')
# [ ] Test the 3 string variables provided by calling w_start_test()

test_string_1='welcome'.lower()
test_string_2='I have $3'.lower()
test_string_3='With a function it\'s efficient to repeat code'.lower()

def w_start_test():
    if test_string_1.startswith('w'):
        print(test_string_1,'starts with "w"')
    else:
        print(test_string_2,'does not start with "w"')

    if test_string_2.startswith('w'):
        print(test_string_2,'starts with "w"')
    else:
        print(test_string_2,'does not starts with "w"')

    if test_string_3.startswith('w'):
        print(test_string_3,'starts with "w"')
    else:
        print(test_string_3,'does not start with "w"')

   print(w_start_test())

这里有很多问题,我会尽力回答

由于某些原因,您正试图打印出您的函数,这将只是试图返回函数的类型,而函数的类型是None。那不会归还任何东西

据我所知,您想要比较许多不同的字符串,有几种方法可以做到这一点,但以下是我的解决方案:

将3个字符串放入如下列表:

test_strings = ['welcome'.lower(),'I have $3'.lower(),'With a function it\'s efficient to repeat code'.lower()]
我们创建的函数与您已经创建的函数相同,但包含参数:

def w_start_test(test_string_list):

    for string in test_string_list:
        if string.startswith('w'):
            print(string,'starts with "w"')
        else:
            print(string,'does not start with "w"')

    return
此函数接受一个参数test_string_list并循环遍历此列表中的所有对象,并执行您提供的比较。然后我们什么也不归还,因为我不确定你想要归还什么

假设您想返回“已完成”,您可以这样做:

test_strings = ['welcome'.lower(),'I have $3'.lower(),'With a function it\'s efficient to repeat code'.lower()]

def w_start_test(test_string_list):

    for string in test_string_list:
        if string.startswith('w'):
            print(string,'starts with "w"')
        else:
            print(string,'does not start with "w"')

    return 'Completed Test'


def __main__():
    ValueOfTest = w_start_test(test_strings)
    print(ValueOfTest)

功能有点复杂。您正在寻找的解决方案如下:

def w_start_test(alpha):
    if alpha.lower().startswith("w"):
        print("The word starts with 'w'")
    else:
        print("The word doesn't start with 'w'")

w_start_test(test_string_1)
w_start_test(test_string_2)
w_start_test(test_string_3)

我试图找到正确的答案。我想我是这样做的

这是我的问题解决方案的变体

test_string_1 = "welcome"
test_string_2 = "I have $3"
test_string_3 = "With a function it's efficient to repeat code"
# [ ] create a function w_start_test() use if & else to test with startswith('w')
# [ ] Test the 3 string variables provided by calling w_start_test()
if test_string_1.lower().startswith('w'):
    print('this string starts with \'w\'')
else:
    pass

if test_string_2.lower().startswith('w'):
    print('this string starts with \'w\'')
else:
    print('this string doesn\'t start with \'w\'')

if test_string_3.lower().startswith('w'):
    print('this string starts with \'w\'')
else:
    pass
为什么
打印(w\u start\u test())
结尾可能重复?