【问题标题】:how to use conditionals (multiple IF and ELSE statements) in a function using PYTHON如何在使用 PYTHON 的函数中使用条件(多个 IF 和 ELSE 语句)
【发布时间】: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())

【问题讨论】:

标签: python function conditional


【解决方案1】:

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

由于某种原因,您试图打印出您的函数,这只会尝试返回函数的类型,即 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)

【讨论】:

    【解决方案2】:

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

    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)
    

    【讨论】:

      【解决方案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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-18
        • 1970-01-01
        • 1970-01-01
        • 2014-09-06
        • 2021-10-12
        • 2021-12-22
        • 1970-01-01
        相关资源
        最近更新 更多