-1

I'm new to programming and i'm taking a course on edx.org.

i'm having issues with using conditionals in a function. each time i call the function it gives me the output i desire but also shows "NONE" at the end. is there any way i can use return keyword in the code? below is the question and my code.

###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())
James Z
  • 12,209
  • 10
  • 24
  • 44

3 Answers3

0

There are a number of questions here, I'll try to answer them.

For some reason, you are attempting to print out your function, this will just attempt to return the type of the function which is None. That won't return anything.

From my understanding you are wanting to compare many different strings, there are a few ways you can do that but here's my solution:

You take your 3 strings, and put them into a list like so:

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

We create our function as you have done so already but include parameters instead:

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

This function takes a parameter, test_string_list and loops through all objects within this list and does the comparisons you have provided. We then return nothing because I am not sure what you want to return.

Let's say you wanted to return 'Completed', you would do this:

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)
0

Functions are slightly complicated. The solution which you are looking for is as below:

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)
0

I was trying to discover the right answer. I think I did so.

Here it's my variant of the problem solution.

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