【发布时间】:2019-01-15 21:59:51
【问题描述】:
我是编程新手,我正在 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())
【问题讨论】:
-
为什么最后是
print(w_start_test())?
标签: python function conditional