Search code examples
pythonfunctionpython-3.xpython-3.5definitions

How to have if and elif in functions


First off. My code:

UserInput = ("null") #Changes later

def ask_module(param, param2):

    elif UserInput == (param):
        print(param2)






while True:

    UserInput = input()
    UserInput = UserInput.lower()
    print()

    if UserInput == ("test"):
    print("test indeed")

    ask_module("test2", "test 2")

I am not that good at coding, so this is probably something that I have done really wrong

This post seems a bit duchy, since I almost just have code, but I have absolutely no idea on how to make this work.

What the code looks like without shortening:

while True:

    UserInput = input()
    UserInput = UserInput.lower()
    print()

    if UserInput == ("inventory"):
        print("You have %s bobby pin/s" %bobby_pin)
        print("You have %s screwdriver/s" %screwdriver)

    elif UserInput == ("look at sink"):
        print("The sink is old, dirty and rusty. Its pipe has a bobby pin connected")
    else:
        print("Did not understand that")

EDIT: I see that it might be hard to see what I'm asking.

I'm wondering how I can shorten my original code


Solution

  • I found a solution, just stop using elif entirely.

    Example:

    userInput = "null"
    
    
    def ask_question(input, output):
        if userInput == (input):
            print(output)
        else: pass
    
    
    while True:
        userInput = input()
        ask_question("test","test")
        ask_question("test2", "test2")
        ask_question("test3", "test3")