#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Please help my code...


    Code:
    x = raw_input("say something:")
    def kill(x):
        if x.isalpha() == True:
            print "BONER!"
        elif not x.isalpha:
            print "Game Over!"    
        else: 
            return True
            pass
    kill(x)    
    y = raw_input("What now?!!")  
    def kill_2(y):
        if y.isalpha() == True:
            print "Jo Momma!"
        elif  not y.isalpha():
            print "Game Over!"
        else:
            return True
            pass
    kill_2(y) 
    k = raw_input("Well... why so quiet?")
    def kill_3(k):
        if k.isalpha() == True:
            print "Asswipe Yeah I said it to you. YOU ARE AN ASSWIPE!"
        elif not k.isalpha():
            print "Game Over!"
        else:
            return True
            pass
    kill_3(k)
    v = raw_input("SPEAK! OR DIE!") 
    def kill_4(v):
        if v.isalpha() == True:
            print "shut up! Dumbass!"
        elif  not v.isalpha():
            print "Game Over!"
        else:
            return True
            pass
    im trying to make a game that involves you having a random missguided talk with the console. i want it to stop running after they input a number i want only letters, but it wont stop after game over. gah! little help... there i changed it but im a novice at coding, im was practicing alittle and goofing off on codecademy labs..
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Well... there are a ton of problems here but one of the first is this:
    Code:
    if x != x.isalpha:
    does not mean/do what you think it does.

    You want:
    Code:
    if not x.isalpha():
    Let's start with that,
    -Mek
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    i fixed it...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Don't edit the above code. Make a new attempt and repost. Anyway, you didn't change it the way I said actually.

    Now you are writing:
    Code:
    if l not l.isalpha():
    This is not what I posted above. I wrote:
    Code:
    if not x.isalpha():
    Look carefully.

    Note: Using lower-case 'L' as a variable name is considered bad form as depending on the font it is impossible to tell from other characters (same goes for 'o' or 'O' and a few others I'm sure I forgot). In general if your variables aren't simple counters, they should have actual descriptive names.

    -Mek

    Edit: You have a lot of fixing left to be done. For instance, in this code:
    python Code:
    def kill_2(y):
        if y.isalpha() == True:
            print "Jo Momma!"
        elif  not y.isalpha():
            print "Game Over!"
        else:
            return True
            pass
    when exactly does that else block run? If you answered never, then you are correct. Also your logic is redundant. If your if statement checks if y.isalpha is true, any elif or else blocks that come below it will only run if it isn't.
    Last edited by Mekire; May 7th, 2013 at 02:20 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    i was trying to create a fail instance if the letters in the input isnt letters...


    EDIT: i was trying anything to make the code work becasue it wouldnt do much when i was coding...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Negative.
    Code:
    >>> help(str.isalpha)
    Help on method_descriptor:
    
    isalpha(...)
        S.isalpha() -> bool
        
        Return True if all characters in S are alphabetic
        and there is at least one character in S, False otherwise.
    You probably mean this:
    python Code:
    def kill(x):
        if x.isalpha():
            print "BONER!"
        else:
            print "Game Over!"
    but note there is nothing in your control flow that is going to stop the game. It simply prints "Game Over!" and then proceeds to the next part of the program as normal.

    -Mek
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    This may be more what you are trying to do, but I'd say you have a bit of studying to do:
    python Code:
    import random
     
    ABUSE = [("say something:","BONER!"),
             ("What now?!!","Jo Momma!"),
             ("Well... why so quiet?","Asswipe Yeah I said it to you. YOU ARE AN ASSWIPE!"),
             ("SPEAK! OR DIE!","shut up! Dumbass!")]
     
    def kill(banter):
        speak = raw_input(banter[0])
        if speak.isalpha():
            print(banter[1])
            return True
        else:
            print "Game Over!"
     
    if __name__ == "__main__":
        next_one = ABUSE[0]
        go = True
        while go:
            go = kill(next_one)
            next_one = random.choice(ABUSE)

    -Mek
    Last edited by Mekire; May 7th, 2013 at 02:56 AM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    thanks man im really new to this... i was mostly playing around in the codelab on codecademy and i thought it would be cool to create what i was trying to create.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Look at my previous post. You need a while loop. Also your functions all do the exact same thing except for a change of dialogue. If you have to cut and paste code then you need to re-examine your approach.

    -Mek
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    im sorry but i want to understand the code you posted, i know it uses list but im not sure how it can ask for input and then get the respond.... and the go varible with the while loop.
    EDIT: i was trying to get better at coding by practice in the codelab
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Ok so let's look closer then:

    (code changed slightly to be more (Monty) Pythonic)
    python Code:
    import random
     
    ABUSE = [("WHAT DO YOU WANT?",
                  "Don't give me that, you snotty-faced heap of parrot droppings!"),
             ("Shut your festering gob, you tit!","Your type really makes me puke."),
             ("Stupid git!!","You vacuous, coffee-nosed, maloderous, pervert!!!")]
     
    def kill(prompt,response):
        speak = raw_input(prompt).lower()
        if speak == "i came here for an argument":
            print("Oh, I'm sorry. This is abuse.")
        elif speak:
            print(response)
            return True
        else:
            print("Good day sir!!")
     
    if __name__ == "__main__":
        back,forth = ABUSE[0]
        go = True
        while go:
            go = kill(back,forth)
            back,forth = random.choice(ABUSE)

    First is of course our imports; here we import random as we are going to want to use random.choice later. Next I define a CONSTANT, ABUSE (the capital letters represent my bound word as a programmer that it is not going to change later). This constant is a list of (prompt,response) tuples.

    Then we define our function. The function takes as arguments a prompt and a response (how convenient). Firstly within the function we get the user input passing our prompt to raw_input; next we check if the input matches a specified string, is a non-empty string, or finally everything else (IE an empty string). The function returns true for the elif, but in the other two cases the function returns None.

    Now lets look at the bottom of the code. First you see:
    Code:
    if __name__ == "__main__":
    This means that code found here will only run if the program is run as main. This is a good habit to build early. Next I assign the first tuple in our constant to two new names. Then I set go to True.

    We now enter our while loop. As long as go stays True the loop continues to run. go is then reassigned the return value from our function (which is called with the two recently assigned variables). Then we randomly reassign the two variables to another pair in our constant ABUSE.

    Now if our function returned True earlier, go will have the value True and our loop will run again; if not, the while loop exits and our program ends.

    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo