簡體   English   中英

如何使用 PYTHON 在函數中使用條件(多個 IF 和 ELSE 語句)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM