Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old September 16th, 2003, 05:15 PM
Baltor's Avatar
Baltor Baltor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 67 Baltor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m
Reputation Power: 5
Question HELP! Problem with dictionary!

I have the following code:
Code:
self.weaponbase = {'1':'2000', '2':'8000', '3':'18000', '4':'32000', '5':'50000', '6':'72000', '7':'98000', '8':'128000', '9':'162000', '10':'200000'}
if self.weaponbonus == 99:
        self.weaponbonus = random.randint(1, 10)

baseprice = self.weaponbase[self.weaponbonus]

So if it generates 2, baseprice should equal 8000.
But, it's not working... Anyone, know whats wrong?
Thanks in advance.
__________________
Before you criticize someone, walk a mile in their shoes, that way when you do criticize them, you're a mile away and you have their shoes!

Reply With Quote
  #2  
Old September 16th, 2003, 05:48 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ok this is obviously part of a class method.. would be nice ff you posted the full class or even the whole method but not very important just a note fore the future.

As far as I can see 'self.weaponbonus' can never be 99 so the if statment would never be executed.. and therefor the keyname may not match one of the values in your dict.

I'm assuming you're getting a KeyError when you run this?

You'd also get a KeyError if you where to use the int returned by randint as keys because the keys in your dict are char's not int's.

Anyway What is this surposed to do Baltor? Maybe I can help more then..

Have fun,
Mark.

Last edited by netytan : September 17th, 2003 at 02:59 AM.

Reply With Quote
  #3  
Old September 16th, 2003, 06:05 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ok i'm not sure if this is what's surposed to happen but i seem to be working ok, like i said i'm not sure what this script is surposed to do, soooo, i took a guess..

>>> import random
>>> d = {'1':'2000', '2':'8000', '3':'18000', '4':'32000', '5':'50000', '6':'72000', '7':'98000', '8':'128000', '9':'162000', '10':'200000'}
>>> i = d[str(random.randint(1, len(d.keys())))]
>>> i
'2000'
>>>

In the example 'i' is being assigned the value or a random key in the 'd' dict.

This could be better done by using int values for your keys so we don't have to type-cast the values to a string (str()).

Also the random number is between 1 and the total number of keys in the dict (len(d.keys())) so if shouldn't be possible to get a KeyError if the keys are named correctly..

Hope this helps
Mark.

Reply With Quote
  #4  
Old September 16th, 2003, 06:16 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
A better way to do what netytan did would be to do:
Code:
i = d[random.choice(d.keys())]


This way you don't care WHAT the keys are (as they don't even have to be the same type).

Also, if you are just going to use '1', '2', ... as your keys ... just use a list.

Reply With Quote
  #5  
Old September 16th, 2003, 06:20 PM
Baltor's Avatar
Baltor Baltor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 67 Baltor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m
Reputation Power: 5
This is the whole script:
Code:
# DND Magic Weapon Generator
# By Evan Patterson

import random
from Tkinter import *

class App:
    def __init__(self, master):

        frame = Frame(master)

        self.weaponbonus = random

        # Dictionaries
        self.meleecom = {'dagger':'302', 'greataxe':'320', 'greatsword':'350', 'kama':'302', 'longsword':'315', 'light mace':'305', 'heavy mace':'312', 'nunchacku':'302', 'quarterstaff':'600', 'rapier':'320', 'scimitar':'315', 'shortspear':'302', 'siangham':'303', 'bastard sword':'335', 'short sword':'310', 'dwarven waraxe':'330'} 
        self.ranged = {'throwing axe':'308', 'heavy crossbow':'350', 'light crossbow':'335', 'dart':'300.5', 'javelin':'301', 'shortbow':'330', 'composite shortbow':'375', 'mighty composite shortbow (+1 Str bonus)':'450', 'mighty composite shortbow (+2 Str bonus)':'525', 'sling':'300', 'longbow':'375', 'composite longbow':'400', 'mighty composite lonbow (+1 Str bonus)':'500', 'mighty composite lonbow (+2 Str bonus)':'600', 'mighty composite lonbow (+3 Str bonus)':'700', 'mighty composite lonbow (+4 Str bonus)':'800'}
        self.uncom = {'orc double axe':'660', 'battleaxe':'310', 'spiked chain':'325', 'club':'300', 'puching dagger':'302', 'falchion':'375', 'dire flail':'690', 'heavy flail':'315', 'light flail':'308', 'gauntlet':'302', 'spiked gauntlet':'305', 'glaive':'308', 'greatclub':'305', 'guirsame':'309', 'halberd':'310', 'halfspear':'301', 'gnome hooked hammer':'620', 'light hammer':'301', 'hand axe':'306', 'kukri':'308', 'heavy lance':'310', 'light lance':'306', 'longspear':'305', 'morning star':'308', 'net':'320', 'heavy pick':'308', 'light pick':'304', 'ranseur':'310', 'sap':'301', 'scythe':'318', 'sickle':'306', 'two bladed sword':'700', 'trident':'315', 'dwarven urgrosh':'650', 'warhammer':'312', 'whip':'301'}
        self.weaponbase = {'1':'2000', '2':'8000', '3':'18000', '4':'32000', '5':'50000', '6':'72000', '7':'98000', '8':'128000', '9':'162000', '10':'200000'}
        self.meleespecial = {'Defending':'1', 'Flaming':'1', 'Frost':'1', 'Shock':'1', 'Ghost touch':'1', 'Keen':'1', 'Mighty Cleaving':'1', 'Spell Storing':'1', 'Throwing':'1', 'Bane':'2', 'Disruption':'2', 'Flaming Burst':'2', 'Icy Burst':'2', 'Shocking Burst':'2', 'Thundering':'2', 'Wounding':'2', 'Holy':'2', 'Unholy':'2', 'Lawful':'2', 'Chaotic':'2', 'Speed':'4', 'Brilliant Energy':'4', 'Dancing':'4', 'Vorpal':'5', 'Binding':'1', 'Fortunate':'1', 'Crazed':'1', 'Harmony':'1', 'Shrinking': '1', 'Ki Focus':'1', 'Merciful': '1', 'Sure Striking': '1', 'Vicious': '1', 'Corrosive': '1', 'Impact': '1', 'Screaming': '1', 'Sweeping': '1', 'Everbright':'2', 'Disarming':'2', 'Acidic Burst':'2', 'Balanced':'2', 'Commanding':'2', 'Eager':'2', 'Fierce':'2', 'Grasping':'2', 'Heartfinder':'2', 'Proficient':'2', 'Radiant':'2', 'Shadowstrike':'2', 'Stunning':'2', 'Force':'3', 'Energy Aura':'3', 'Knockback':'3'}
        self.rangedspecial = {'Flaming':'1', 'Frost':'1', 'Shock':'1', 'Returning':'1', 'Distance':'1', 'Bane':'2', 'Holy':'2', 'Unholy':'2', 'Lawful':'2', 'Chaotic':'2', 'Flaming Burst':'2', 'Icy Burst':'2', 'Shocking Burst':'2', 'Speed':'4', 'Brilliant Energy':'4', 'Binding':'1', 'Fortunate':'1', 'Shrinking':'1', 'Mericful':'1', 'Seeking':'1', 'Sure Striking':'1', 'Corrosive':'1', 'Impact':'1', 'Precise':'1', 'Screaming':'1', 'Screaming':'1', 'Everbright':'2', 'Acidic Burst':'2', 'Commanding':'2', 'Fierce':'2', 'Balanced':'2', 'Proficient':'2', 'Radiant':'2', 'Shadowstrike':'2', 'Stunning':'2', 'Force':'3', 'Energy Aura':'3', 'Knockback':'3'}
        self.special = {'Defending':'A defending weapon allows the wielder to transfer some or all of the swords enhancment bonus to his ac as a special bonus that stacks with all the others. As a free action, the wielder chooses how to allocate the weapons enhancment bonus at the start of his turn before using the weapon, and the effect to AC lasts unti his next turn.', 'Flaming':'Upon command, a flaming weapon in sheathed in fire. The fire does not harm the wielder. Flaming weapons deal +1d6 points of fire damage on a succcessful hit.',}
#Etc (too long of a list)
        self.banelist = ['Aberation', 'Animal', 'Beast', 'Construct', 'Dragon', 'Elemental', 'Fey', 'Giant', 'Magical beast', 'Monstrous humanoid', 'Oozes', 'Chaotic outsider', 'Evil outsider', 'Good outsider', 'Lawful outsider', 'Plant', 'Shapechanger', 'Undead', 'Vermin', 'Humanoids (choose subtype)']

        # Labels
        self.label1 = Label(master, text="DND Magic Weapon Generator")
        self.label1.grid(row=0, column=0, columnspan=5)

        # Radiobuttons
        self.radio1 = Radiobutton(master, text="+1", variable=self.weaponbonus, value=1)
        self.radio1.grid(row=1, column=0)
        self.radio2 = Radiobutton(master, text="+2", variable=self.weaponbonus, value=2)
        self.radio2.grid(row=1, column=1)
        self.radio3 = Radiobutton(master, text="+3", variable=self.weaponbonus, value=3)
        self.radio3.grid(row=1, column=2)
        self.radio4 = Radiobutton(master, text="+4", variable=self.weaponbonus, value=4)
        self.radio4.grid(row=1, column=3)
        self.radio5 = Radiobutton(master, text="+5", variable=self.weaponbonus, value=5)
        self.radio5.grid(row=1, column=4)
        self.radio6 = Radiobutton(master, text="+6", variable=self.weaponbonus, value=6)
        self.radio6.grid(row=2, column=0)
        self.radio7 = Radiobutton(master, text="+7", variable=self.weaponbonus, value=7)
        self.radio7.grid(row=2, column=1)
        self.radio8 = Radiobutton(master, text="+8", variable=self.weaponbonus, value=8)
        self.radio8.grid(row=2, column=2)
        self.radio9 = Radiobutton(master, text="+9", variable=self.weaponbonus, value=9)
        self.radio9.grid(row=2, column=3)
        self.radio10 = Radiobutton(master, text="+10", variable=self.weaponbonus, value=10)
        self.radio10.grid(row=2, column=4)
        self.radioran = Radiobutton(master, text="Random", variable=self.weaponbonus, value=99)
        self.radioran.grid(row=3, column=0, columnspan=5)                    

        # Texbox
        self.text = Text(master, height=6, width=30, state=DISABLED)
        self.text.grid(row=4, column=0, columnspan=5)

        # Button
        self.button = Button(master, text="Generate!", command=self.generate)
        self.button.grid(row=5, column=0, columnspan=5)

    def generate(self):
        
        self.text.config(state=NORMAL)
        self.text.delete(1.0, END)
        self.text.insert(1.0, END, "Name:")
        self.text.insert(2.0, END, "Price:")
        self.text.insert(3.0, END, "Description:")
        
        # Weapon type
        self.weapontype = random.randint(1, 10)
        if self.weapontype < 8:
            weaponname = random.choice(self.meleecom.keys())
            weaponcost = self.meleecom[weaponname]
            weapona = self.meleespecial
        elif self.weapontype == 8:
            weaponname = random.choice(self.uncom.keys())
            weaponcost = self.uncom[weaponname]
            weapona = self.meleespecial
        else:
            weaponname = random.choice(self.ranged.keys())
            weaponcost = self.ranged[weaponname]
            weapona = self.rangedspecial

        if self.weaponbonus == 99:
            self.weaponbonus = random.randint(1, 10)

        # Prices            
        baseprice = self.weaponbase[self.weaponbonus]
        totalprice = baseprice + weaponcost
        self.text.insert(2.0, END, totalprice)

        # Bonuses
        speciallist = self.special
        enhancebonus = random.randint(1, 5)
        specialbonus = weaponbonus - enhancebonus
        while specialbonus > 0:
            specialability = random.choice(weapona.keys())
            if specialability < specialbonus:
                weapondescription = weapona[specialability]
                specialbonus  = specialbonus - specialability
                self.text.insert(3.0, END, weapondescription)
        self.text.config(state=DISABLED)
        
root = Tk()
app = App(root)
root.title("Magic Weapon Generator")
root.mainloop()

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > HELP! Problem with dictionary!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway