Domanda

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!

È stato utile?

Soluzione 2

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.

Altri suggerimenti

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)()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top