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

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Help with While loops


    Hi everyone,
    I'm working on a project with a friend at school to help us both get better at coding for our computing A-level.
    This project is a choose your own adventure game, and I'm trying to refine a battle system, I have a working one, but I thought it would be neater and/or easier to turn the whole thing into a function using def. Unfortunately it isn't working and I don't know why. I've searched about to find an answer, but cannot find one.

    Here is my code, it is written in Python 2.7

    Code:
    import random, time, sys, os
    
    possibleclasses = ["warrior", "paladin", "mage", "rogue", "bard"]
    
    print("These are the rules and instructions: \nPlease follow the game and do as it says do not try to break it\nPlease take care in spelling commands as they are important\nEach scenario will tell you what to type to choose an option e.g [sneak] and also what ability it uses\nEnjoy the game\n")
    class Stats:
        def __init__(self, _classchoose):
            self.int = 2
            self.str = 2
            self.agi = 2
            self.char = 2
            self.hp = 8
            
            if (_classchoose == "warrior"):
                self.str += 4
                self.hp += 3
            if (_classchoose == "paladin"):
                self.int += 2
                self.str += 2
                self.char += 2
                self.hp += 6
            if (_classchoose == "mage"):
                self.int += 6
                self.char += 2
            if (_classchoose == "rogue"):
                self.agi += 4
                self.char += 2
                self.hp += -2
                self.str += 1
            if (_classchoose == "bard"):
                self.char += 6
                self.int += 2
                self.agi += 1
                
    classchoose = str.lower(raw_input("Choose your class; \nPaladin\nWarrior\nMage\nRogue\nBard\nChoose Class: "))
    while classchoose not in possibleclasses:
        classchoose = str.lower(raw_input("Choose your class; \nPaladin\nWarrior\nMage\nRogue\nBard\nChoose Class: "))
    
    you = Stats(classchoose)
    
    stats = ("Your stats are: \n" + "Intelligence " + str(you.int) + "\nStrength " + str(you.str) + "\nAgility " + str(you.agi) + "\nCharisma " + str(you.char) + "\nHealth " + str(you.hp))
    print(stats)
    
    def fight(enemy_hp, enemy_str):
        you_str = (random.randint(1,6) + you.str)
        while you_str < enemy_hp:
            print("Rolling...")
            time.sleep(2)
            print("You deal " + str(int(you_str)) + " damage to the goblin!")
            time.sleep(2)
            if enemy_str == 0: #miss chance! Woo!
                print("The goblin missed!!!")
            else:
                print("The goblin deals " + str(int(enemy_str)) + " damage to you!")
            enemy_hp = enemy_hp - you_str
            you.hp = you.hp - gob_str
            you_str = (random.randint(1,6) + you.str)
            enemy_str = random.randint(0,6)
            time.sleep(2)
            if you.hp <= 0:
                print("The goblin has impaled you on his magnificent weapon. Sucks to be you!")
                time.sleep(5)
                sys.exit()
    
    
    gob_hp = random.randint(10, 20)
    gob_str = random.randint(0, 6)
    
    fight_options = ["sneak", "fight"]
    goblin = random.randint(10, 20)
    Fight = raw_input("You have come across a wandering goblin with " + str(int(gob_hp)) + " health, You may attempt to [sneak] past (this uses agility), or [fight] the goblin (this uses strength)? ")
    while Fight not in fight_options:
        Fight = raw_input("You have come across a wandering goblin with " + str(int(gob_hp)) + " health, You may attempt to sneak past (sneak, uses agility), or fight the goblin (fight, uses strength)? ")
    
    if Fight == "fight":
        fight(gob_hp, gob_str)
    else:
        print("You manage to evade it")
    Any help is appreciated, so thank you in advance!

    Comments on this post

    • b49P23TIvg agrees : Yes it fails. Needs 2 space characters before "S" in "Sucks".
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    514
    Rep Power
    33
    I don't know what "it isn't working" means. It "works" as it was programmed to. I've added some print statments to show what is happening.
    Code:
    import random, time, sys, os
    
    possibleclasses = ["warrior", "paladin", "mage", "rogue", "bard"]
    
    print("""These are the rules and instructions:
    Please follow the game and do as it says do not try to break it
    Please take care in spelling commands as they are important
    Each scenario will tell you what to type to choose an option
          e.g [sneak] and also what ability it uses
    Enjoy the game
    """)
    class Stats:
        def __init__(self, _classchoose):
            self.int = 2
            self.str = 2
            self.agi = 2
            self.char = 2
            self.hp = 8
            
            if (_classchoose == "warrior"):
                self.str += 4
                self.hp += 3
            if (_classchoose == "paladin"):
                self.int += 2
                self.str += 2
                self.char += 2
                self.hp += 6
            if (_classchoose == "mage"):
                self.int += 6
                self.char += 2
            if (_classchoose == "rogue"):
                self.agi += 4
                self.char += 2
                self.hp += -2
                self.str += 1
            if (_classchoose == "bard"):
                self.char += 6
                self.int += 2
                self.agi += 1
                
    classchoose = ""
    while classchoose not in possibleclasses:
        classchoose = str.lower(raw_input("Choose your class; \nPaladin\nWarrior\nMage\nRogue\nBard\nChoose Class: "))
    
    you = Stats(classchoose)
    
    stats = ("Your stats are: \n" + "Intelligence " + str(you.int) + "\nStrength " + str(you.str) + "\nAgility " + str(you.agi) + "\nCharisma " + str(you.char) + "\nHealth " + str(you.hp))
    print(stats)
    
    def fight(enemy_hp, enemy_str):
        you_str = (random.randint(1,6) + you.str)
        while you_str < enemy_hp:
            print("Rolling...")
            time.sleep(2)
            print("You deal " + str(int(you_str)) + " damage to the goblin!")
            time.sleep(2)
            if enemy_str == 0: #miss chance! Woo!
                print("The goblin missed!!!")
            else:
                print("The goblin deals " + str(int(enemy_str)) + " damage to you!")
            enemy_hp = enemy_hp - you_str
            you.hp = you.hp - gob_str
            print(you_str, enemy_hp)
            you_str = (random.randint(1,6) + you.str)
            enemy_str = random.randint(0,6)
            time.sleep(2)
            if you.hp <= 0:
                print("The goblin has impaled you on his magnificent weapon. Sucks to be you!")
                time.sleep(5)
                sys.exit()
        print("you_str >= enemy_hp", you_str, enemy_hp)
    
    gob_hp = random.randint(10, 20)
    gob_str = random.randint(0, 6)
    
    fight_options = ["sneak", "fight"]
    ##  goblin = random.randint(10, 20)  ## not used
    Fight = ""
    while Fight not in fight_options:
        Fight = raw_input("You have come across a wandering goblin with " + str(int(gob_hp)) + " health, You may attempt to sneak past (sneak, uses agility), or fight the goblin (fight, uses strength)? ")
    
    if Fight == "fight":
        fight(gob_hp, gob_str)
    else:
        print("You manage to evade it")
    Last edited by dwblas; February 27th, 2014 at 05:22 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0
    Just as a quick glance, it looks like you should change your if statements to if-else.

    If you are only going to choose one among many, then don't make the computer program look through all of the other if statements. if-else accomplishes that.

    pseudocode:
    if (userInput ==1) print "hello"
    else if (userInput == 2) print "joe"
    else if (userInput == 3) print "boat"

    If the computer sees that the user entered 1, it can print "hello" and then stop looking at the other statements below it.

    If you just use a bunch of if statements, the computer will still have to look at all of them, only to find out it didn't need to look at them after all.

    That's not central to the question you are asking. But while you have the car on the lift, you might as well take care of this part of your code.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    514
    Rep Power
    33
    Even better is to use a dictionary to avoid redundant code, and put the input in the class so each additional user is automatically asked the right questions, but that may be beyond the OP's comfort zone. And int, str, etc are already used by Python.
    Code:
    import random, time, sys, os
    
    print("""These are the rules and instructions:
    Please follow the game and do as it says do not try to break it
    Please take care in spelling commands as they are important
    Each scenario will tell you what to type to choose an option
          e.g [sneak] and also what ability it uses
    Enjoy the game
    """)
    
    class Stats:
        def __init__(self):
            self.intel=2
            self.stn=2
            self.agi=2
            self.char=2
            self.hp=8
            
            self.character_input()
    
        def character_input(self):
            ## a dictionary pointing to a list of numbers to add
            ## to the traits fields
            ##  list = [intelligence, stn, agi, char, hp]
            character_dict = {"warrior":[0, 4, 0, 0, 3],
                              "paladin":[2, 2, 0, 2, 6],
                              "mage":[2, 0, 0, 2, 0],
                              "rogue":[0, 2, 4, 2, 2],
                              "bard":[2, 0, 2, 2, 0] }
                
            classchoose = ""
            while classchoose not in character_dict:
                classchoose = raw_input("""Choose your class; 
    Paladin
    Warrior
    Mage
    Rogue
    Bard
    
    Choose Class: """).lower()
    
            print("Your stats are:")
            add_to_list = character_dict[classchoose]
            self.intel += add_to_list[0]
            print("Intelligence = %d" % (self.intel))
            self.stn += add_to_list[1]
            print("Strength = %d" % (self.stn))
            self.agi += add_to_list[2]
            print("Agility = %d" % (self.agi))
            self.char += add_to_list[3]
            print("Charisma = %d" % (self.char))
            self.hp += add_to_list[4]
            print("Health = %d\n" % (self.hp))
    
    def fight(enemy_hp, enemy_str):
        you_str = (random.randint(1,6) + you.stn)
        while you_str < enemy_hp:
            print("Rolling...")
            time.sleep(2)
            print("You deal " + str(int(you_str)) + " damage to the goblin!")
            time.sleep(2)
            if enemy_str == 0: #miss chance! Woo!
                print("The goblin missed!!!")
            else:
                print("The goblin deals " + str(int(enemy_str)) + " damage to you!")
            enemy_hp = enemy_hp - you_str
            you.hp = you.hp - gob_str
            print(you_str, enemy_hp)
            you_str = (random.randint(1,6) + you.stn)
            enemy_str = random.randint(0,6)
            time.sleep(2)
            if you.hp <= 0:
                print("The goblin has impaled you on his magnificent weapon. Sucks to be you!")
                time.sleep(5)
                sys.exit()
        print("you_str >= enemy_hp", you_str, enemy_hp)
    
    you = Stats()
    
    gob_hp = random.randint(10, 20)
    gob_str = random.randint(0, 6)
    
    fight_options = ["sneak", "fight"]
    ##  goblin = random.randint(10, 20)  ## not used
    Fight = ""
    while Fight not in fight_options:
        Fight = raw_input("You have come across a wandering goblin with " + str(int(gob_hp)) + " health, You may attempt to sneak past (sneak, uses agility), or fight the goblin (fight, uses strength)? ")
    
    if Fight == "fight":
        fight(gob_hp, gob_str)
    else:
        print("You manage to evade it")

IMN logo majestic logo threadwatch logo seochat tools logo