1

Im new to python so I decided to help myself learn by creating a basic game using python 3! My problem occurs whenever I try to use "def". When I try to run this program it skips all user input entirely because of the def. The goal of using a function in this case would be to return the player to the where it presents the two doors. Maybe its an issue related to indentation? If anyone could give it a shot and see if you can spot the error your help would be greatly appreciated! :D

def sec1 ():
print ("You have two doors in front of you. Do you choose the door on the left or right?")
room1 = input('Type L or R and hit Enter.')

if room1 == "L":
    print ("********")
    print ("Good choice",name)
elif room1 == "R":
    print ("********")
    print ("Uh oh. Two guards are in this room. This seems dangerous.")
    print ("Do you want to retreat or coninue?")
    roomr = input('Type R or C and hit enter.')

if roomr == "R":
    print ("Good choice!")
    sec1()
Chris
  • 19
  • 4
  • Possible duplicate of [How to make a chain of function decorators?](https://stackoverflow.com/questions/739654/how-to-make-a-chain-of-function-decorators) – Chris Jun 12 '17 at 04:40

3 Answers3

2

You have an indentation problem. Indentation matters in Python. According to the PEP8 styling guideline it is recommended to use 4 spaces instead of tabs for indentation. Also you are missing the name variable.

Below is a quick fix:

def sec1 ():
    print("You have two doors in front of you. Do you choose the door on the left or right?")
    room1 = input('Type L or R and hit Enter.')

    name = "Player Name"

    if room1 == "L":
        print("********")
        print("Good choice", name)

    elif room1 == "R":
        print("********")
        print("Uh oh. Two guards are in this room. This seems dangerous.")
        print("Do you want to retreat or coninue?")
        roomr = input('Type R or C and hit enter.')

        if roomr == "R":
            print("Good choice!")
            sec1()

sec1()

Why we have sec1() at then end?

Functions are like machines. It does nothing by it's own. Somebody has to operate it. sec1() (notice the parenthesis) at the end is sending a signal to start executing the function sec1 defined at the top.

I think the best way to learn is to put break points and use the debugger to learn which way the program flows.

Run the program in a debug mode and click on the icons to step through, step over etc. It sounds complicated but it is very easy and saves you a lot of time once you know how to do this feature.

Mathematical Functions

Maybe it is a bit off topic to mention Mathematical Functions here but I think, it's completely worth it. Functions in programming languages are heavily inspired by Mathematical Functions, however, in most of the programming languages these days (except functional programming languages like Haskell, F# etc) the concepts of the original Mathematical Functions are quite deviated over the year.

In Mathematics, the output of a function purely depends upon it's input and does not modify the values outside the function, however, in most of the programming languages this is not always the case and sometimes it can be a source of run time errors.

Tips

As you are a beginner, I highly recommend to use a proper IDE (Integrated Development Environment) if you haven't already. PyCharm has a free community version. IDEs come with a PEP8 style checker, debugger, profiler etc and help you to learn Python much more easily.

Eddie
  • 1,043
  • 8
  • 14
  • Your answer seems to imply, particularly to a beginner, that using 4 spaces is critical with Python. This isn't the case at all. It's good practice, recommended in PEP8, but not at all necessary. This user does have an indentation problem, but tabs vs spaces isn't part of their problem. – Chris Larson Dec 30 '16 at 02:25
  • 1
    @Chris Larson That's true. It is just a PEP8 recommendation. I used to mix up tabs with spaces all the time when I was learning Python and get confused. – Eddie Dec 30 '16 at 02:35
  • Ha! Me, too. And I didn't meant to discourage your point, just that it might have been misinterpreted as the source of the problem by someone new to all this. I actually use tabs, and convert to spaces when I need to share my code. :/ Bad, bad practice, I know. On second thought, maybe it isn't so bad to scare the new folks into thinking they have to use 4 spaces. :D – Chris Larson Dec 30 '16 at 02:38
  • Thanks Eddie! Just installed PyCharm and its working out great! Ive got another question if you dont mind...Why do we put the function at the bottom of the code as well, and would I do that if I were to have more than one function? For example if I had another function just after sec1 would I also place this new functions name at the bottom of the code, like it is where it says sec1()? Thanks for your help! – Chris Dec 31 '16 at 08:02
  • 1
    @Chris Think of the definition of the function as kind of a tool in your toolkit. It doesn't actually do anything until you reach out and use it. So, if you `def hammer()` you now have a hammer, but you aren't using it. Once it's `def`ined, though, you can hammer() away with it. Does that make sense? – Chris Larson Dec 31 '16 at 08:18
  • 1
    @Chris Basically, the stuff at the top, with the `def` is _not_ a thing that _does_ stuff. It simply `define`s the thing to do. When you want to _do_ it, you use the bit after the `def` by itself. – Chris Larson Dec 31 '16 at 08:21
  • Calling `sec()` might seem redundant in this case. This next example might make it more clear: type in `def print_my_name(name):`, then hit `enter` and type 4 spaces and then `print("Your name is:", name)` Now type `enter` a couple of times and type `print_my_name(input("What's your name?"))` See if that makes it more clear as to why functions are useful. Try adding another line that reads `print_my_name("Chris")`. You can write functions that do something and use them over and over again with different variables passed in to them. – Chris Larson Dec 31 '16 at 08:32
  • Sorry for the convoluted `type this, then enter, then this`. Comments make it hard to show multi-line code. – Chris Larson Dec 31 '16 at 08:33
  • 1
    @Chris Larson Im gonna have to update my answer. I reckon that's more like it. – Eddie Dec 31 '16 at 08:36
  • Hey there, thank you guys for all the help! My apologies for the late reply. Anyway, I understand that functions are used to automate a task that the programmer does not want to retype over and over. And yes that makes sense that defining a function does not really tell the computer to execute any commands. I have added to the game theres more problems. – Chris Jan 05 '17 at 23:55
  • So I have to define a function after an if statement. So i have an if statement, then I have several lines of printing sentences, then i have the def function so that i can define only a few printed sentences and then prompt the user with a question. The problem is that after the def function I have to indent the print statements or the user prompt. Im afraid this is leading to the program failing to execute this block of code. I can go ahead and show you guys the specific problem on another question, Id love if you could take a look. – Chris Jan 05 '17 at 23:58
  • @Chris not necessarily you have to define a function after an if statement. Please try creating a new question. I would encourage you to follow some tutorials, if you haven't already. This is a good one. https://www.tutorialspoint.com/python3/index.htm – Eddie Jan 06 '17 at 00:28
1
def sec1 ():
    print ("You have two doors in front of you. Do you choose the door on the left or right?")
    room1 = input('Type L or R and hit Enter.')

the function body should be indented

delta
  • 3,778
  • 15
  • 22
1

In Python, indentation is very important. Here's an example of your code with proper indentation (And a couple of liberties take on my part):

def sec1 ():
    print ("You have two doors in front of you. Do you choose the door on the left or right?")
    name = input('Enter your name.')
    room1 = input('Type L or R and hit Enter.')

    if room1 == "L":
        print ("********")
        print ("Good choice",name)
    elif room1 == "R":
        print ("********")
        print ("Uh oh. Two guards are in this room. This seems dangerous.")
        print ("Do you want to retreat or coninue?")
        roomr = input('Type R or C and hit enter.')
        if roomr == "R":
            print ("Good choice!")
        elif roomr == "C":
            print ("Run!")

sec1()

Chris Larson
  • 1,684
  • 1
  • 11
  • 19
  • The indentation is definitely something to look out for in python! This was just a section of my code take out but I appreciate your contributions nonetheless! – Chris Dec 31 '16 at 07:59
  • Yes! It's likely the number one problem for folks getting started! I hope this gives you a good idea of what you should be watching for. You'll get used to it really quickly. I'd second the suggestion that you use something like PyCharm. It'll make a big difference, and once you learn the environment a little, will let you run through your code step by step and see what's going on at any point. Welcome aboard! – Chris Larson Dec 31 '16 at 08:16