How do you make a function run as soon as it is created?

This is the place for queries that don't fit in any of the other categories.

How do you make a function run as soon as it is created?

Postby hootdoog » Tue Jan 07, 2014 6:01 pm

How do you make a function run as soon as it is created?

Many Thanks!
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: How do you make a function run as soon as it is created?

Postby stranac » Tue Jan 07, 2014 6:26 pm

Just call it when you define it.
But do you would want to do that?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: How do you make a function run as soon as it is created?

Postby hootdoog » Tue Jan 07, 2014 6:31 pm

I need to get the "def combat()" section, to run without "combat()" being typed.

Code: Select all
import random
monster = ["Bandito", "Floating-Shovel", "Dragon", "Fairy", "Seahorse", "Mr-Cheung", "Sponge", "Goblin", "Orge", "Wolf", "Pixie", "Kelan", "Charlie", "Gelatinous-Cube", "Demon", "Werewolf", "Flamingo", "Hipster", "Pikcahu", "Evil-Stingbee", "Brine-Shrimp", "DingleFlump", "Kitten", "Frickle-Clumpzinger", "Fox"]
combat = "true"
away = "You run like a coward!"
print (" I============================I ")
print (" I== Welcome To TurkeyQuest ==I ")
print (" I============================I ")
print ("")
name = input("What is your name young turkey: ")
print ("")
print ("Thats a stupid name", name)
print ("")
weapon = input(" To your left you see a sword, to your right a bow what do you choose: ")
if weapon == "1337":
    print ("")
    print ("Woah a L33t-Sword, Havent seen one in years!")
    print ("")
if weapon == "Sword":
    print ("")
    print ("Haha, swords suck!")
    print ("")
elif weapon == "Bow":
    print ("")
    print("Haha, Bows suck!")
    print ("")
elif weapon == "sword":
    print ("")
    print ("Haha, swords suck!")
    print ("")
elif weapon == "bow":
    print ("")
    print("Haha, Bows suck!")
else:
    print ("Choose a real weapon!")
print ("")
print ("Type 'combat(), to continue!")
print ("")
def combat():
    rounds = 0
    health = 100
    enemy = 50
    fighting = "yes"
    print("After wandering you encounter a", random.choice(monster) )
    attack = "Attack"
    print ("")
    while attack == "Attack":
            if attack == "run":
                print (away)
                break
            enemy = enemy - random.randint(1,15)
            print ("You attack with your", weapon)
            print ("Enemy Health: ", enemy )
            print ("")
            if attack == "run":
                print (away)
                break
            if enemy <= 0:
                print ("You slay the enemy with grace!")
                print ("")
                rounds + 1
                combat()
            health = health - random.randint(1,10) # WOO SPECIAL RANDOM THINGS YEAH WOO IT WORKS!!!!! (Note: Im really happy I got that to work!)
            print ("Enemy attacks")
            print ("Your Health: ", health )
            if health <= 0:
                print ("You have been slain in battle! Rounds lasted: ", rounds)
                break
            print ("")
            attack = input("What do you! [Attack, Run]: ")
            if attack == "run":
                print (away)
                break
            print ("")
            print("You run like a coward!")
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: How do you make a function run as soon as it is created?

Postby stranac » Tue Jan 07, 2014 6:36 pm

Why? Also, you can't do that.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: How do you make a function run as soon as it is created?

Postby hootdoog » Tue Jan 07, 2014 6:40 pm

It seems as though its the easiest way to go about it :P any alternative and easier ways?
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: How do you make a function run as soon as it is created?

Postby stranac » Tue Jan 07, 2014 7:04 pm

Just call the function when you need it.

You should also not be defining a function in the middle of your other code like that.
The main reason for using a function, other than reusability, is to make your code more structured.
If you just define function in the middle of your code, you gain nothing...

You should probably be doing something like:
Code: Select all
def combat():
    # do stuff

# other stuff
if need_to_do_combat:
    combat()


A good use for a function would be getting read of repeated code, such as the part where you print that the weapon sucks.
I would rewrite that as:
Code: Select all
weapon_descriptions = {
    '1337': 'Woah a L33t-Sword, Havent seen one in years!',
    'sword': 'Haha, swords suck!',
    'bow': 'Haha, Bows suck!',
}

def print_weapon_description(weapon):
    # note the use of .lower(), so we don't need to check for different capitalization
    desc = weapon_descriptions.get(weapon.lower(), 'Choose a real weapon!')
    # printing newlines like this just seems nicer to me...
    print('\n' + desc + '\n')


You also might want to get started on using classes before you make this game more complicated(it will make it much easier)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: How do you make a function run as soon as it is created?

Postby hootdoog » Tue Jan 07, 2014 7:09 pm

Ok, In which case, how would I change the code so it just automatically runs?, without the use of a function
hootdoog
 
Posts: 14
Joined: Wed Dec 04, 2013 5:22 pm

Re: How do you make a function run as soon as it is created?

Postby stranac » Tue Jan 07, 2014 7:23 pm

You wouldn't.
Python will not run code, unless you tell it to.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: How do you make a function run as soon as it is created?

Postby micseydel » Tue Jan 07, 2014 11:06 pm

You could use a decorator to tell it to, and not have to call it, if you were evil.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1354
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How do you make a function run as soon as it is created?

Postby Kebap » Wed Jan 08, 2014 10:50 am

hootdoog wrote:Ok, In which case, how would I change the code so it just automatically runs?, without the use of a function


Yes you can run code without using functions. Just write everything below each other, like you mostly did. Disadvantage is: Code gets unreadable. Recommendation is: Learn using functions.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: How do you make a function run as soon as it is created?

Postby Somelauw » Thu Jan 09, 2014 2:14 am

hootdoog wrote:How do you make a function run as soon as it is created?

Many Thanks!


What is the original problem that you are trying to solve?
Making a function run without typing the function name isn't possible and is probably not going to gain you anything.
Is there any reason you don't want to write "combat()" in your code?
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 71
Joined: Tue Feb 12, 2013 8:30 pm


Return to General Coding Help

Who is online

Users browsing this forum: Crimson King and 4 guests