0

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

Kim André
  • 129
  • 1
  • 8
  • Make what work? what is happening in you program? Also, indentation is KEY in Python. – Idos Jan 09 '16 at 18:24
  • 3
    You can't start an if-elif chain in one place and finish it in another. And Python forces you to indent things carefully. If that's not enough help, you should work your way through a Python tutorial, such as [this one](https://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python_3) or [this one](http://www.learnpython.org/). – zwol Jan 09 '16 at 18:25
  • OK, I made a text adventure, but It quickly became repetitions, I want to short the repetition. I'll edit in my original code. – Kim André Jan 09 '16 at 18:27
  • `if`, `elif`, and `else` work the same way in functions that they do outside of functions. `def f(x): if x > 10 print "Big" else print "Small"` – tblznbits Jan 09 '16 at 18:27
  • I edited in my original code. – Kim André Jan 09 '16 at 18:30
  • The indentation got a bit messed up when copying into the editor – Kim André Jan 09 '16 at 19:12
  • Compression is generally more effective when there is redundancy. For example, if you have a code block repeated twice, put it in a function and call the function instead. Your code has very little redundancy. – Reti43 Jan 10 '16 at 00:04
  • There are many, many more "elif UserInput (input): print(output). That's the part I want to shorten – Kim André Jan 10 '16 at 00:11
  • I suggest you edit your question to make it more clear. It seems the first code block is irrelevant; you just need a code snippet with a chain of if statements and to simply ask how to shorten it. Describing that in the title would also help future readers come across with it if they are searching for the same problem. – Reti43 Jan 10 '16 at 01:23
  • If you are trying to write a text adventure, you will have a much better time if you use one of the special-purpose languages designed for that task, e.g. Inform, Ren'Py, Twine. See https://emshort.wordpress.com/how-to-play/writing-if/ for further advice. – zwol Jan 13 '16 at 17:02

2 Answers2

0

If all your elif blocks have the same pattern, you can take advantage of this.

You can create a dictionary for the text you want to print and then do away with the conditionals.When it comes to choosing which one to print, you simply fetch the relevant text using its corresponding key. You use the get(key, default) method. If there is no key in the dictionary, the default value will be returned. For example,

choices = {'kick': 'Oh my god, why did you do that?',
           'light him on fire': 'Please stop.',
           'chainsaw to the ribs': 'I will print the number %d',
          }

user_input = input().lower()

# individually deal with any strings that require formatting
# and pass everything else straight to the print command
if user_input == 'chainsaw to the ribs':
    print(choices[user_input] % 5)
else:
    print(choices.get(user_input, 'Did not understand that.'))
Reti43
  • 9,656
  • 3
  • 28
  • 44
0

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")
Kim André
  • 129
  • 1
  • 8