![](/img/trans.png)
[英]How to use python list comprehension for multiple if elif and else statements
[英]how to use conditionals (multiple IF and ELSE statements) in a function using PYTHON
我是编程新手,我正在 edx.org 上一门课程。
我在函数中使用条件时遇到问题。 每次我调用该函数时,它都会给我我想要的输出,但最后也会显示“NONE”。 有什么办法可以在代码中使用 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.