how to use conditionals (multiple IF and ELSE statements) in a function using PYTHON

python if statement multiple conditions
python if statement string
python multiple if statements on one line
python if-else one line
python conditional assignment
w3schools if statement python
python if true statement
if statement with input python

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

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)

If Statements, If Else Statements & Nested If Statements: Python, This tutorial will take you through writing conditional statements in the Python For this, we will use an else if statement, which is written in Python as elif . Stack Overflow Public questions and (multiple IF and ELSE statements) in a function using PYTHON. i'm having issues with using conditionals in a function

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)

Python IF, ELSE, ELIF, Nested IF & Switch Case Statement, Learn how to use If, Else, Elif, Nested IF and Switch Case Statements with examples. Conditional statements are handled by IF statements in Python. function(argument){ switch(argument) { case 0: return "This is Case  Python Conditions and If statements. Python supports the usual logical conditions from mathematics: Equals: a == b. Not Equals: a != b. Less than: a < b. Less than or equal to: a <= b. Greater than: a > b. Greater than or equal to: a >= b.

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

8. Compound statements, Function and class definitions are also syntactically compound statements. thus there are no ambiguities (the 'dangling else ' problem is solved in Python by requiring nested if statements to be indented). The if statement is used for conditional execution: The context manager's __enter__() is loaded for later use. Python 3 Conditional Statements: If, If Else and Nested If Statements. In a Python program, the if statement is how you perform this sort of decision-making. It allows for conditional execution of a statement or group of statements based on the value of an expression.

Conditional Statements in Python – Real Python, Master if-statements step-by-step and see how to write complex decision The else and elif Clauses; One-Line if Statements; Conditional Expressions For what it's worth, many programmers who have been used to languages Of course, there is a built-in function max() that does just this (and more) that you could use. Using filter() function with a conditional lambda function (with if else) filter() function accepts a callback() function and a list of elements. It iterates over all elements in list and calls the given callback() function on each element. If callback() returns True then it appends that element in the new list.

3.1. If Statements, More Conditional Expressions¶. All the usual arithmetic comparisons may be made, but many do not use standard mathematical symbolism, mostly for lack of  We have already looked if-elif-else statements in previously. if-elif-else requires conditions in order evaluate. These conditions may simple True , False or comparisons. The other way is we can define complex conditionals in order to evaluate. Simple conditions are just single statement conditions where we do not need grouping or multiple or

how to use conditionals (multiple IF and ELSE statements) in a , 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  Python If, Else and Elif Conditional Statements In the last tutorial , we studied about the logical and relational expressions and tried to understand their usage with help of various examples. We also saw examples about how multiple relational expressions can be put together with the help of logical operators to create meaningful logical

Comments
  • Possible duplicate of What is the formal difference between "print" and "return"?
  • why print(w_start_test()) at the end?