it says the following: Write a function that converts the time to 24hr format. Examples [CODE] >>> time24hr('12:34am') '0034hr' >>> time24hr('12:15pm') '1215hr' [/CODE] so i wrote the following: [CODE] def time24hr(tstr): a = tstr.split(':') am = {'12':'00','1':'01','2':'02','3':'03','4':'04','5':'05','6':'06', '7':'07','8':'08','9':'09','10':'10','11':'11'} pm = {'12':'12','1':'13','2':'14','3':'15','4':'16','5':'17','6':'18', '7':'19','8':'20','9':'21','10':'22','11':'23'} if 'am' in tstr: return am[a[0]] + a[1][:2] + 'hr' elif 'pm' in tstr: return pm[a[0]] + a[1][:2] + 'hr' [/CODE] But what would have been a better way to do it? i imagine their is an inbuilt method of doing this, but is there a more efficient way to do this without inbuilts? and could you show …

Member Avatar
Member Avatar
+0 forum 5

Here's a simple tutorial on dictionaries in Python: Dictionaries: Dictionaries are similar to other compound types except that they can use any immutable type as an index. One way to create a dictionary is to start with the empty dictionary and add items. The empty dictionary is denoted {}: [CODE]end2sp = {} end2sp[ "one" ] = "uno" end2sp[ "two" ] = "dos"[/CODE] We can print the current value of the dictionary in the usual way: [CODE]print eng2sp # --> { "one":"uno", "two":"dos" }[/CODE] The elements of a dictionary appear in a coma-separated list. Each entry contains an index and a …

Member Avatar
Member Avatar
+5 forum 16

**Introduction** Hi guyz today I am going to writing a tutorial on dictionaries in Python....As per as getting comments from my readers I'll try to make this tutorial short.... **Layout** 1.What are dictionaries? 2.Why dictionaries? 3.How to declare/make dictionaries in Python? 4.What all you can do with a dictionary? 5.Interesting Functions? 6.Dictionary Methods? **What are dictionaries?** Dictionaries are simply another type of Sequence in Python.. Like in English dictionaries you search for a word and its meaning … Same happens in python... Dictionaries in Python have a “key” and a “value of that key”... That may seem strange for the …

Member Avatar
Member Avatar
+0 forum 6

Hi. I´m writing my first python program - a text adventure game. I want to have a list of possible things a dog could eat, what´s bad about them, how bad they are. So, I thought I´d do this: badfoods = [] keys = ['Food','Problem','Imminent death'] food1 = ['alcohol', 'alcohol poisoning', 0] food2 = ['anti-freeze', 'ethylene glycol', 1] food3 = ['apple seeds', 'cyanogenic glycosides', 0] There are actually around 40 foods I want to include. I know I can do this: badfoods.append(dict(zip(keys,food1))) badfoods.append(dict(zip(keys,food2))) etc. Or, I could have written it another way for all 40 foods: [{'Food':'alcohol', 'Problem':'alcohol poisoning', 'Imminent …

Member Avatar
Member Avatar
+0 forum 1

How would I find the amount of characters in a string and then use that to print a certain amount of some character that you choose. For example: [CODE=Python] a = 'string' # this holds 6 characters print '*' * # amount of characters in string [/CODE]

Member Avatar
Member Avatar
+0 forum 2

While correcting errors in someone else's code, I decided to completely rewrite the original script, and improve upon it. This is the basic code for making 3D arrays using dictionaries. [code] ar = { (x,y,z):0 # creating the array for x in range(3) for y in range (3) for z in range (3)} ar [2,1,0] = 777 # random value ar [2,2,1] = 2345 # random value for x in sorted(ar): #output print(str(x) + " --- " + str(ar[x])) #print output [/code]

Member Avatar
Member Avatar
+0 forum 3

[B][I][I][U]Introduction:-[/U][/I][/I][/B] [COLOR="Red"] Hi guyz today I am going to writing a tutorial on dictionaries in Python....As per as getting comments from my readers I'll try to make this tutorial Short....[/COLOR] [B][I][U] Layout:-[/U][/I][/B] [COLOR="Green"] 1.What are dictionaries? 2.Why dictionaries? 3.How to declare/make dictionaries in Python? 4.What all you can do with a dictionary? 5.Interesting Functions? 6.Dictionary Methods? [/COLOR] [B][I][U]What are dictionaries? [/U][/I][/B] Dictionaries are simply another type of Sequence in Python.. Like in English dictionaries you search for a word and its meaning … Same happens in python... Dictionaries in Python have a “key” and a “value of that key”... That …

Member Avatar
Member Avatar
+0 forum 2

I'm working on learning GUI development, I'm attempting to utilize the GUI for a user to search the dictionary: my code so far is[CODE]from tkinter import * class Interface(Frame): def __init__(self, master): super(Interface, self).__init__(master) self.grid() self.create_widget() def create_widget(self): self.pwrd= Label(self, text='Address Book') self.pwrd.grid(row=0, column=2) self.searchlbl= Label(self, text='Enter Search Query:') self.searchlbl.grid(row=2, column=0) self.search= Entry(self) self.search.grid(row=2,column=1,columnspan=1) self.subsearch= Button(self, text='Submit', command=self.searchfor) self.subsearch.grid(row=2,column=2) self.searchresult= Text(self, width=40, height=10, wrap=WORD) self.searchresult.grid(row=3,column=0, columnspan=2, rowspan=2) def searchfor(self): query=str(self.search) self.searchresult.delete(0.0,END) if query in ab.book: result=ab.book[str(query)] self.searchresult.insert(0.0,result) else: result='Query not found.' self.searchresult.insert(0.0,result) class AddressBook(object): """AddressBook Features""" def __init__(self, book): self.book=book def search(self,query): query=query.lower() if query in self.book: return self.book[query] if …

Member Avatar
Member Avatar
+0 forum 3

So I've finally fixed up the functionality of an address book I've been writing (not on to utilizing a GUI yet of course though.)However to save or reclaim the data from the last use of the address book I, obviously, need to write and reclaim it from a .txt file, I understand how to write and read, however I'm not sure how to write so when it is read I can make it back into a dictionary. Any ideas? here's how I'm writing so far. [CODE]for line in AB.keys(): line=line+'\n ' print(line,file=f) for line in AB.values(): for string in line: …

Member Avatar
Member Avatar
+0 forum 3

I am currently having problems displaying a file correctly once i have written a dictionary to the file. For this program the input file needs to have the format: ID: Date: Dayskept: ProductName e.g. 1:12/12/2011:12:A This is fine the first time I read the example file into a dictionary, but once i save the dictionay into a new file and try to open this file i get the output: 1:"date":12/12/2011, "life":12, "name":A Is there an easy way to format the data in the dictionary before it is written to file? Thanks for any advice given :) [CODE]def loadProduct(fileName): global cheeseDictionary …

Member Avatar
Member Avatar
+0 forum 6

Hi I am new to the forums, and somewhat new to python. I'm trying to make a Text Based Adventure game. I don't have an error, but a bug. This is the code: [code]import descriptions inventory = [] gold = 0 acts = {"north":"hi"} def start(acts): print(descriptions.start) rm_a(acts) def rm_a(acts): print("FOYER") print(descriptions.foyer) acts={"north":print("You can't go that way."), "east":rm_b(acts), "south":print("You can't go that way."), "west":print("You can't go that way."), "take book":inventory.append(descriptions.book[0])} prompt(acts) def rm_b(acts): print("DINING ROOM") print(descriptions.dining) acts={"north":print("You can't go that way."), "east":rm_c(acts), "south":rm_e(acts), "west":rm_a(acts), "take sandwich":inventory.append(descriptions.sandwich[0])} prompt(acts) def rm_c(acts): print("KITCHEN") print(descriptions.kitchen) acts={"north":print("You can't go that way."), "east":print("You can't go that …

Member Avatar
Member Avatar
+0 forum 7

Hi I was wondering how to extract info from a data file, and place it in a dictionary. Say you're given a file, in the following format: [CODE]tomCruise # Username Tom Cruise # Name Los Angeles, CA # Location http://www.tomcruise.com # URL Official TomCruise.com crew tweets. We love you guys! # Bio (line 1) Visit us at Facebook! #Bio (line 2) ENDBIO katieH # Followers NicoleKidman # Followers END # End[/CODE] There are multiple users in the data file, but all of them have the same format. I initially started by creating an empty dictionary, and then wanted to use …

Member Avatar
+0 forum 0

Hello, I'm trying to update a dictionary using variable in a class. Something looking like this : [code=python] class testClass(object): def __init__(self): self.data0 = 0 self.data1 = 1 self.data2 = 2 self.data3 = 3 self.d = dict( data0 = self.data0, data1 = self.data1, data2 = self.data2, data3 = self.data3 ) def updateData(self,data): self.data0 = data[0] self.data1 = data[1] self.data2 = data[2] self.data3 = data[3] for key in self.d: print key + ' : ' + str(self.d[key]) if __name__ == '__main__': test = testClass() data = [4,5,6,7] test.updateData(data) [/code] Obviously the dictionary is not updated at all using this method. Is …

Member Avatar
Member Avatar
+0 forum 3

The End.