0

I'm trying to create a text game that presents scenarios in a random order. Based upon a user's answer, certain pre-defined statistics should increase. I took a stab at it, but my loop function will not work (among other things). I attempted to incorporate information from this thread: call list of function using list comprehension

Here is my code:

import random

# A class for the user character. Each character has four stats, which start at zero.
class Character(object):

    def __init__(self, sass, intuition, despotism, panache):
        self.sass = sass
        self.intuition = intuition
        self.despotism = despotism
        self.panache = panache

    sass = 0
    intuition = 0
    despotism = 0
    panache = 0

    # a function to check the current stat level of the character.
    def all_check(self):
        print "Your level of sass is %s." % self.sass
        print "Your level of intuition is %s." % self.intuition
        print "Your level of despotism is %s." % self.despotism
        print "Your level of panache is %s." % self.panache

# I assume that these four "Event" functions should be instances of a class due to each Event's commonalities, but I can't understand how to implement.
def Event1():

    print """An attractive woman smiles at you and your friend
        from across the bar. Your friend confesses that his passions are arouse, but that he is too shy to do  anything. What do you do?"""
    print "1. Convince the woman to talk to your friend." #intuition
    print "2. Tell the woman to talk to your friend... or else." #despotism
    print "3. Inform the woman that your friend has herpes, but that you'd love to take her out." #sass
    print "4. Why fight? Let's all go back to her place and get weird." #panache, intuition

    inp = raw_input(">>> ")

        if inp == '1':
            Character.intuition += randint(2,4)
        elif inp == '2':
            Character.despotism += randint(2,4)
        elif inp == '3':
            Character.sass += randint(2,4)
        elif inp == '4':
            Character.panache += randint(1,3)
            Character.intuition += randint(1,3)
        else:
            print "You fail."

# To save space, I'm leaving out Events2~4. They have the same structure as Event1

# Put all the Event functions into a list.
events = [
        Event1,
        Event2,
        Event3,
        Event4,
        ]
# A function to cycle through all the Events in random order.       
def play(self):
    list = shuffle(events)
    for x in list:
        x()

# Call a class function to create a unique user.
userName = raw_input("What's your name? ")


# Call function to begin the game.
play()

Character.all_check()

I also realize that the Events are more appropriate as instances of a class, but I'm having trouble understanding Zed's (LPTHW) explanation of classes. Any thoughts are welcome.

Community
  • 1
  • 1
Kwhitejr
  • 2,206
  • 5
  • 29
  • 49
  • 1
    You're using a class as a bag of functions and variables. You should use modules for that. Classes are for scenarios in which you need *multiple* objects with similar behaviors. – Kevin Apr 20 '15 at 03:12
  • 1
    Please ask a specific question or state the problem you are having and constrain your post to a [mcve](http://stackoverflow.com/help/mcve) – wwii Apr 20 '15 at 03:57
  • @Kevin hasn't seen a singleton recently~ But what makes you think he isn't going to use multiple `Character`s? It's data with associated functionality, so a class isn't that bad of a choice. Don't need the class-level variable definitions, of course. – a p Apr 20 '15 at 04:03
  • @ap: That may well be his (her?) intention, but he clearly needs to (re)learn OOP principles first. – Kevin Apr 20 '15 at 13:17
  • @wwii: Thanks for the heads up on posting protocol/etiquette. Will do better. – Kwhitejr Apr 25 '15 at 21:28

0 Answers0