Author Topic: Python Miniprograms  (Read 557 times)

Python
Python is the best programming language ever C:
It's simple easy, and really useful :)
For a full documentation on how to learn python
google "Python Scripting Tutorial" c:

Miniscripts
My infinite pi generator


from __future__ import print_function
def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    while (1):
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2


digits = make_pi()
pi_list = []
my_array = []

for digit in make_pi():
    print(digit, end='')


my pyfighter script


#PyFighter 4.0 Alpha
#By Brian Smith
#Idea by Ottosparks
#No rights are reserved
#TIME TO CODE!
import os, math, random
#Variables!
monsterlist = {"0": "Death",
               "1": "Brian Smith",
               "2": "Ottosparks",
               "3": "Badspot",
               "4": "Giant",
               "5": "Goblin",
               "6": "Wizard",
               }
monsterhealth = {"Death": 4000,
                 "Brian Smith": 9001,
                 "Ottosparks": 400,
                 "Badspot": 2000,
                 "Giant": 3000,
                 "Goblin": 750,
                 "Wizard": 900,
                 }
attacklist = {"1": "Slice",
              "2": "Stab",
              "3": "Thrash",
              "4": "Cut",
              }
attackdamage = {"Slice": 20,
                "Stab": 40,
                "Thrash": 10,
                "Cut": 5,
                }

debugmode = 0
level = 1
monsterHP = 0
currentHP = 100
gold = 100

######################
#       Monsters     #
######################
def debugMonsters():
    for i in range(0,10):
        print monsterlist[str(i)]
    else:
        print "Monster Debug Complete"

def monsterAppear(name):
    global potion, gold, monsterHP, currentHP, monsterlist, health, level, monsterhealth, attackdamage
    health = monsterhealth[name]
    monsterHP = health
    print "-----------------------"
    print "A Monster has Appeared!"
    print "-----------------------"
    print "A " + name + " has appeared with " + str(health) + " HP!"
    print "Which option do you want to use? Type one of the following:"
    print "1 - Attack  |  2 - Defend  |  3 - Heal  |  4 - Use Potion"
    input = str(raw_input("Command - "))
    if input == "1":
        oldMonsterHP = monsterHP
        attackMethod = attackMethodRequest()
        currentHP,monsterHP = attackMonster(currentHP,monsterHP,attackdamage[attackMethod])
        damage = oldMonsterHP - monsterHP
        if currentHP < 1:
            print "You have died. The game now closes."
            return quitGame()
        elif monsterHP < 1:
            level = level + 1
            gold = gold + random.randint(1,100)
            print "You have killed the monster, you have also leveled up to level " + str(level) + ". \nYou also obtained gold. You now have " + str(gold) + " gold."
            displayOptions()
        else:
            print "Your attack did " + str(damage) + " damage, and the " + name + "\'s health is now " + str(monsterHP) + " and yours is " + str(currentHP) + " "
            nextMove(name)
    elif input == "2":
        coin = random.randint(0,3)
        if coin == 1:
            damage = random.randint(1,50)
            print "Your dodge failed and you were damaged " + str(damage) + " HP"
            currentHP = currentHP - damage
            if currentHP < 1:
                print "You have died. The game now closes."
                return quitGame()
        else:
            print "You dodged the " + name + "\'s attack!"                                             
        nextMove(name)
       
    elif input == "3":
        coin = random.randint(0,1)
        if coin == 1:
            heal = random.randint(15,50)
            currentHP = currentHP + heal
            print "You heal yourself " + str(heal) + " HP, with " + str(currentHP) + " HP now!"
            nextMove(name)
        else:
            damage = random.randint(50,100)
            print "Your heal was interrupted by an attack! You were damaged " + str(damage) + " HP"
            currentHP = currentHP - damage
            if currentHP < 1:
                print "You have died. The game now closes."
                return quitGame()
            nextMove(name)
    elif input == "4":
            if potion < 1:
                print "You have no potions richardhead."
                nextMove(name)
            else:
                potion = potion - 1
                currentHP = currentHP + 100
                print "Your health is now " + str(currentHP) + " and you have " + str(potion) + " potions."
                nextMove(name)
    else:
        print "You had to type 1 2 or 3"
        print "Clearly since you cant do that, you can't play"
        return quitGame()

def attackMethodRequest():
    print "Select a Method of Attacking:"
    print "1 - Slice  |  2 - Stab  |  Thrash - 3  |  Cut - 4"
    method = raw_input("Method - ")
    if method == 1:
        return "Slice"
    elif method == 2:
        return "Stab"
    elif method == 3:
        return "Thrash"
    elif method == 4:
        return "Cut"
    else:
        return "Cut"

def quitGame():
        exit = 1
        while exit > exit + 1:
            print "Exit"
        return exit
def attackMonster(yourHP,otherHP,attackDamage):
    global level, armor, armorStats
    AM = random.randint(100,1000)
    attack = level * attackDamage + AM
    attack = otherHP - attack
    damage = yourHP - random.randint(15,70) + armorStats[armor]
    return damage,attack

def nextMove(name):
    global monsterHP, currentHP, level, attackmethod, gold, potion
    print "Which option do you want to use? Type one of the following:"
    print "1 - Attack  |  2 - Defend  |  3 - Heal  |  4 - Use Potion"
    input = str(raw_input("Command - "))
    if input == "1":
        oldMonsterHP = monsterHP
        attackMethod = attackMethodRequest()
        currentHP,monsterHP = attackMonster(currentHP,monsterHP,attackdamage[attackMethod])
        damage = oldMonsterHP - monsterHP
        if currentHP < 1:
            print "You have died. The game now closes."
            return quitGame()
        elif monsterHP < 1:
            level = level + 1
            gold = gold + random.randint(1,100)
            print "You have killed the monster, you have also leveled up to level " + str(level) + ". \nYou also obtained gold. You now have " + str(gold) + " gold."
            displayOptions()
        else:
            print "Your attack did " + str(damage) + " damage, and the " + name + "\'s health is now " + str(monsterHP) + " and yours is " + str(currentHP) + " "
            nextMove(name)
    elif input == "2":
        coin = random.randint(0,3)
        if coin == 1:
            damage = random.randint(1,50)
            print "Your dodge failed and you were damaged " + str(damage) + " HP"
            currentHP = currentHP - damage
            if currentHP < 1:
                print "You have died. The game now closes."
                return quitGame()
        else:
            print "You dodged the " + name + "\'s attack!"                                             
        nextMove(name)
       
    elif input == "3":
        coin = random.randint(0,1)
        if coin == 1:
            heal = random.randint(15,50)
            currentHP = currentHP + heal
            print "You heal yourself " + str(heal) + " HP, with " + str(currentHP) + " HP now!"
            nextMove(name)
        else:
            damage = random.randint(50,100)
            print "Your heal was interrupted by an attack! You were damaged " + str(damage) + " HP"
            currentHP = currentHP - damage
            if currentHP < 1:
                print "You have died. The game now closes."
                return quitGame()
            nextMove(name)
    elif input == "4":
            if potion < 1:
                print "You have no potions richardhead."
                nextMove(name)
            else:
                potion = potion - 1
                currentHP = currentHP + 100
                print "Your health is now " + str(currentHP) + " and you have " + str(potion) + " potions."
                nextMove(name)
    else:
        print "You had to type 1 2 or 3"
        print "Clearly since you cant do that, you can't play"
        return quitGame()
######################
#Run the Game        #
######################
def startGame():
    setLocation("Home")

def randomMonsterAppear():
    global monsterlist
    monsterAppear(monsterlist[str(random.randint(0,9))])


####################
#Location Stuff    #
####################
currentLocation = ""
monsterWatch = 0
monsterPlaces = {"Home": 0,
                 "Deathly Hollows": 1,
                 "Bridge": 1,
                 "Giants Hole": 1,
                 "Fort Nethgar": 0,
                 "Firey Cave": 1,
                 "Woods": 1,
                 }

deathAttacks = {"Deathly Hollows": 1,
                "Firey Cave": 0,
                "Woods": 0,
                "Bridge": 0,
                "Giants Hole": 0,
                }
bsAttacks = {"Deathly Hollows": 1,
             "Firey Cave": 1,
             "Woods": 0,
             "Bridge": 0,
             "Giants Hole": 0,
             }
ottoAttacks = {"Woods": 1,
               "Bridge": 1,
               "Giants Hole": 0,
               "Firey Cave": 0,
               "Deathly Hollows": 0,
               }
badAttacks = {"Deathly Hollows": 1,
              "Giants Hole": 1,
              "Woods": 1,
              "Bridge": 0,
              "Firey Cave": 0,
              }
wizAttacks = {"Woods": 1,
               "Bridge": 1,
               "Firey Cave": 1,
               "Deathly Hollows": 0,
               "Giants Hole": 0,
               }
giantAttacks = {"Woods": 1,
                "Bridge": 1,
                "Giants Hole": 1,
                "Deathly Hollows": 0,
                "Firey Cave": 0,
                }
gobAttacks = {"Woods": 1,
              "Bridge": 1,
              "Giants Hole": 1,
              "Firey Cave": 1,
              "Deathly Hollows": 0,
              }
attackArray = {"Death": deathAttacks,
               "Brian Smith": bsAttacks,
               "Ottosparks": ottoAttacks,
               "Badspot": badAttacks,
               "Wizard": wizAttacks,
               "Giant": giantAttacks,
               "Goblin": gobAttacks,
               }
locationOptions = {"Home": "Bridge \nFort Nethgar",
                   "Deathly Hollows": "Firey Cave \nFirey Cave",
                   "Bridge": "Giants Hole \nFirey Cave",
                   "Giants Hole": "Bridge \nWoods",
                   "Fort Nethgar": "Home \nWoods",
                   "Firey Cave": "Deathly Hollows \nBridge",
                   "Woods": "Fort Nethgar \nGiants Hole"
                   }
                   
                   
def setLocation(location):
    location = location.strip()
    global monsterlist, currentLocation, monsterPlaces, monsterWatch, deathAttacks, bsAttacks, ottoAttacks, badAttacks, giantAttacks, gobAttacks, attackArray
    if monsterPlaces[location] == 1:
        monsterWatch = 1
    else:
        monsterWatch = 0
    currentLocation = location
    print "You are now in " + location + " "
    if monsterWatch == 0:
        print "Watch out for monsters!"
        displayOptions()
    else:
        coin = random.randint(0,2)
        if coin == 1:
            print "oh stuff..."
            monster = monsterFromLocation(location)
            #print monster
            monsterAppear(monster)
        else:
            print "You got lucky, no monsters appear"
            displayOptions()

def monsterFromLocation(location):
    location = location.strip()
    global monsterlist, currentLocation, monsterPlaces, monsterWatch, deathAttacks, bsAttacks, ottoAttacks, badAttacks, giantAttacks, gobAttacks, attackArray
    num = random.randint(0,6)
    monster = monsterlist[str(num)]
    attarray = attackArray[monster]
    #print attarray
    if attarray[location] == 0:
        return monsterFromLocation(location)
    else:
        #print monster
        return monster

def displayOptions():
    global currentLocation, currentHP, level, locationOptions, gold
    print "------------------------"
    print "Your stats:"
    print "HP: " + str(currentHP) + " "
    print "Level: " + str(level) + " "
    print "Location: " + currentLocation + " "
    print "Gold: " + str(gold) + " "
    if currentLocation == "Fort Nethgar":
        print "------------------------"
        print "Would you like to buy something?"
        print "Type Y/N"
        yn = raw_input("Answer - ")
        if yn == "":
            yn = "N"
        if yn.upper() == "Y":
            market()
           
    options = locationOptions[currentLocation]
    list = options.split("\n")
    print "------------------------"
    print "Where to go:"
    print "1 - " + list[0] + "  |  2 - " + list[1] + " "
    print "Type the number of the place you would like to visit"
    input = int(raw_input("Location - "))
    if input == 1:
        setLocation(list[0])
    elif input == 2:
        setLocation(list[1])
    else:
        setLocation(list[0])

def market():
    global gold, currentHP, potion
    print "---------------------------"
    print "What would you like to buy?"
    print "You have " + str(gold) + " gold."
    print "---------------------------"
    print "1 - Armor  |  2 - Potions  |  3 - Health  |  4 - Quit"
    input = raw_input("Buy - ")
    input = str(input)
    if input == "1":
        buyArmor()
    elif input == "2":
        print "Type in the amount of potions you would like to purchase (100g each):"
        input = raw_input("Amount - ")
        cost = int(input) * int(100)
        if gold < cost:
            print "You need " + str(cost) + " gold."
        else:
            potion = int(potion) + int(input)
            print "You have bought " + str(input) + " potions. You have " + str(potion) + " potions."
            gold = gold - cost
           
    elif input == "3":
        if gold > 50:
            gold = gold - 50
            currentHP = 100
            print "You are healed."
        else:
            print "You need 50 gold for this."
    elif input == "4":
        print "You will go back to locations..."
    else:
        print "You will go back to locations..."

armorStr = {"0": "None",
            "1": "Weak",
            "2": "Medium",
            "3": "Strong",
            }
armorStats = {"0": 0,
              "1": 35,
              "2": 50,
              "3": 75,
              }
armor = str(0)
potion = 0
def buyArmor():
    global gold, armor, armorStr, potion
    print "---------------------------"
    print "What armor do you want?"
    print "1 - Weak  |  2 - Medium  |  3 - Strong"
    print "---------------------------"
    input = raw_input("Armor - ")
    input = str(input)
    if input == "1":
        if gold < 100:
            print "You need atleast 100 gold for this."
        else:
            gold = gold - 100
            print "You now have " + str(gold) + " gold"
            print "and your armor is " + armorStr[input] + " "
            armor = input
    elif input == "2":
        if gold < 200:
            print "You need atleast 200 gold for this."
        else:
            gold = gold - 200
            print "You now have " + str(gold) + " gold"
            print "and your armor is " + armorStr[input] + " "
            armor = input
    elif input == "3":
        if gold < 300:
            print "You need atleast 300 gold for this."
        else:
            gold = gold - 300
            print "You now have " + str(gold) + " gold"
            print "and your armor is " + armorStr[input] + " "
            armor = input
       

startGame()

also the above codes are pretty old.
I just need to keep track of my python programs more lol. THose are the only ones i can find :s

talk about python 'n' stuff here.

Code: [Select]
from urlparse import urlparse

def url_to_id(url):
    try:
        parse = urlparse(url).query
        start = parse.index('v=') + 2
        stop = parse[start:].find('&') + start
        if stop <= start:
            return parse[start:]
        else:
            return parse[start:stop]
    except:
        return None

def dict_to_url(id, dict):
    url = 'http://youtube.com/v/%s?version=3&' % (id)
    if 'version' in dict:
        del dict['version']
    for key, value in dict.iteritems():
        url += '%s=%s&' % (key, value)
    if url[-1] == '&':
        url = url[0:-1]
    return url

if __name__ == '__main__':
    id = raw_input('Video ID or URL: ')
    if 'youtube.com' in id:
        id = url_to_id(id)
    if not id:
        raise Exception
    dict = {'theme': 'dark', 'start': 0, 'showsearch': 1, 'showinfo': 1, 'rel': 1, 'modestbranding': 0, 'loop': 0, 'iv_load_policy': 1, 'hd': 0, 'fs': 0, 'egm': 0, 'disablekb': 0, 'controls': 1, 'border': 0, 'autoplay': 0, 'autohide': 2}
    val = raw_input('Control type ([S]tandard, [L]ight, [C]hromeless): ')
    if type(val) is str:
        if val.lower() == 's':
            pass
        elif val.lower() == 'l':
            dict['theme'] = 'light'
        elif val.lower() == 'c':
            dict['controls'] = 0
        else:
            raise Exception
    else:
        raise Exception
    val = raw_input('Autoplay Video ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['autoplay'] = 1
        elif val.lower() == 'n':
            dict['autoplay'] = 0
        else:
            raise Exception
    else:
        raise Exception
    if dict['controls'] == 1:
        val = raw_input('Autohide Controls ([Y]es, [N]o): ')
        if type(val) is str:
            if val.lower() == 'y':
                dict['autohide'] = 1
            elif val.lower() == 'n':
                dict['autohide'] = 0
            else:
                raise Exception
        else:
            raise Exception
    val = raw_input('Disable Keyboard Controls ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['disablekb'] = 1
        elif val.lower() == 'n':
            dict['disablekb'] = 0
        else:
            raise Exception
    else:
        raise Exception
    """val = raw_input('Enhanced Genie Menu ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['egm'] = 1
        elif val.lower() == 'n':
            dict['egm'] = 0
        else:
            raise Exception
    else:
        raise Exception"""
    val = raw_input('Allow Fullscreen ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['fs'] = 1
        elif val.lower() == 'n':
            dict['fs'] = 0
        else:
            raise Exception
    else:
        raise Exception
    val = raw_input('Attempt Forced HD ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['hd'] = 1
        elif val.lower() == 'n':
            dict['hd'] = 0
        else:
            raise Exception
    else:
        raise Exception
    val = raw_input('Loop Video ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['loop'] = 1
        elif val.lower() == 'n':
            dict['loop'] = 0
        else:
            raise Exception
    else:
        raise Exception
    val = raw_input('Less Branding ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['modestbranding'] = 1
        elif val.lower() == 'n':
            dict['modestbranding'] = 0
        else:
            raise Exception
    else:
        raise Exception
    val = raw_input('Load Related Videos ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['rel'] = 1
        elif val.lower() == 'n':
            dict['rel'] = 0
        else:
            raise Exception
    else:
        raise Exception
    val = raw_input('Show Video Info ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['showinfo'] = 1
        elif val.lower() == 'n':
            dict['showinfo'] = 0
        else:
            raise Exception
    else:
        raise Exception
    val = raw_input('Show Video Search ([Y]es, [N]o): ')
    if type(val) is str:
        if val.lower() == 'y':
            dict['showsearch'] = 1
        elif val.lower() == 'n':
            dict['showsearch'] = 0
        else:
            raise Exception
    else:
        raise Exception
    val = raw_input('Video Start Position (empty for 0): ')
    if len(val):
        dict['start'] = int(val)
    url = dict_to_url(id, dict)
    print url
    raw_input()
« Last Edit: March 25, 2012, 02:51:22 PM by Port »

>"miniprogram"
>442 lines long

although i suppose that it would be longer in torque..

>"miniprogram"
>442 lines long

although i suppose that it would be longer in torque..

442 line -> short