1

I am having trouble with if statements in python. I am making a "game" entirely in dolan speak, excuse the spelling, it meant to be humorous manner. Sorry.

Here is the code:

import time

def menu():
    print ("dogz r a supar hahrd tin 2 matsr it tak yrs 2 mastr ut u nw git 2 exprince it. pik a tin 2 du:\n")
    menu = raw_input("1.)Ply Da Dogi gam\n2.)Halp\n")

    if menu == 1:
        game()

    if menu == 2:
        helpGame()

    if menu < 2:
        print ("dat not 1 ur 2 sry")
        time.sleep(1)
        menu()

def game():
    print ("nuw u ply mi gme u lke it")

def helpGame():
    print ("dis da halp u liek it")
menu()

That doesn't work for me, and I have never had direct function calling work inside of if statements and I have had to implement "seg-ways" which call the function.

Does this work for any of you? Is it possible it is my Python installation? Thanks!

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
Blade Nelson
  • 41
  • 1
  • 2
  • 10
  • 4
    *That doesn't work for me* - you need to be more detailed than that. It's worth pointing out that `raw_input` returns a string - and that you probably want `int(..)` and that... `if menu < 2` will also execute `if menu == 1`... Think about using `elif` statements if criteria should be mutually exclusive... Other than that - not sure what else you're after... – Jon Clements Jul 16 '13 at 23:17
  • 1
    As a side note, calling `menu` recursively from `menu` like this is generally a bad idea. It's better to write this as a loop, which you `break` or `return` on a valid answer, instead of as a function that calls itself on an invalid answer. – abarnert Jul 16 '13 at 23:49

2 Answers2

2

As you add more options, the if statements get unwieldly. Try out this dictionary based structure instead.

def menu():
    print ("dogz r a supar hahrd tin 2 matsr it tak yrs 2 mastr ut u nw git 2 "
           "exprince it. pik a tin 2 du:\n")
    prompt = "1.)Ply Da Dogi gam\n2.)Halp\n"
    {'1': game, '2': helpGame}.get(raw_input(prompt), menu)()
dansalmo
  • 11,506
  • 5
  • 58
  • 53
  • I think it would be clearer (at least to a novice) to put the function-table dict into a variable instead of calling methods on a literal, and probably also to leave the result of `raw_input` in a variable. – abarnert Jul 16 '13 at 23:48
1

raw_input() returns a string, you are using a number in your if statements, change this line:

menu = raw_input("1.)Ply Da Dogi gam\n2.)Halp\n")

to this:

menu = int(raw_input("1.)Ply Da Dogi gam\n2.)Halp\n"))

You will want to look in dealing with error conditions next though.

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • Thanks! I taught myself python and did not know this. Such a life savor! (Side note, I just made the if statement if menu == "1"...It works just fine.) Cheers mate! – Blade Nelson Jul 16 '13 at 23:25
  • You are doing the opposite by changing the comparison from int to string, but this won't work for the `if menu < 2`, which is why I went with the `int()` approach :) – Jason Sperske Jul 16 '13 at 23:27
  • @JasonSperske: Actually, `"1" < "2"` is just as true as `1 < 2`, so that's not a problem. But that doesn't mean there isn't a problem: as soon as you get to 9 entries, `"10" < "9"` is _also_ true, and you probably don't want that… Obviously your approach is better, it's just a matter of explaining why it's obvious. :) – abarnert Jul 16 '13 at 23:47