- Strength to Increase Rep
- +8
- Strength to Decrease Rep
- -2
- Upvotes Received
- 16
- Posts with Upvotes
- 16
- Upvoting Members
- 11
- Downvotes Received
- 0
- Posts with Downvotes
- 0
- Downvoting Members
- 0
Re: I don't know of one. This code requires a radial coordinate. [code=Python]class Pt(object): def __init__(self, x=0.0, y=0.0, z=0.0): self.x = x self.y = y self.z = z def __str__(self): return '(%0.4f, %0.4f, %0.4f)' % (self.x, self.y, self.z) def __repr__(self): return 'Pt(%f, %f, %f)' % (self.x, self.y, self.z) def __add__(self, other): … | |
Re: I would like to share this function that converts a number in base 10 to any base between 2 and 36. Originally it was limited to bases 2-16. [code=Python]import string def convDecToBase(num, base, dd=False): if not 2 <= base <= 36: raise ValueError, 'The base number must be between 2 … | |
Re: Please use code tags when posting code. Are you receiving an error message? If so, please post it. Initialize a maximum score variable (maxscore). Loop on the file object [ICODE]for line in file_obj:[/ICODE]. Strip and split each line [ICODE]lineList = line.strip().split()[/ICODE]. If the value of the score is greater than … | |
Re: Here's a possibility: [code=Python]import string numbers = set('1234567890') alpha = set(string.ascii_letters) mixed = numbers.add(alpha) f = open(file_name) for i, line in enumerate(f): # i is the line number beginning with 0 s1 = set(line.strip()) if s1.issubset(numbers): # do something elif s1.issubset(alpha): # do something elif s1.issubset(mixed): # do something else: … | |
Re: This may give you some ideas: [code]>>> 'abcdefg'.count('a') 1 >>> import string >>> string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz' >>> s = 'I have to write a program that will decode a Ceasar cypher with any possible rotation length' >>> dd = dict(zip(string.ascii_lowercase, [s.count(c) for c in string.ascii_lowercase])) >>> dd['e'] 8 >>> [/code] | |
Re: A Tkinter Entry widget can be configured to "show" a character such as "*". [code]Tkinter.Entry(pwframe,width=25,show="*")[/code] | |
Re: I suggest using a dictionary. Using name "seq" instead for "list" avoids masking built-in function [I]list()[/I].[code]>>> seq = ['v', 3.0,1, 'etc'] >>> dd = {} >>> for item in seq: ... v = dd.get(type(item), 0) ... dd[type(item)] = v+1 ... >>> [dd[obj] for obj in (int, float, str)] [1, 1, … | |
Re: [QUOTE=dseto200;738085]I set the amount of tries to less than 3 ,however when I make more than 3 guesses it won't up the message. What did I do wrong? [code=Python]# Guess My Number # The computer picks a random number import random print "\tWelcome to 'Guess my number'!:" print "\nI'm thinking … | |
Re: I know this thread was marked "solved", but I think the following is a somewhat simpler solution: [code]f.write(",".join(["%s,%s" % (item.real, item.imag) for item in cpl1]))[/code] | |
Re: Using minidom: [code] from xml.dom import minidom doc = '''<?xml version="1.0" ?> <countries> <country> <city capital="Paris">Paris</city> </country> <country> <city capital="Helsinki">Helsinki</city> </country> <country> <city capital="Bogota">Bogota</city> </country> </countries>''' docXML = minidom.parseString(doc) for elem in docXML.getElementsByTagName("city"): elem.removeAttribute("capital") print docXML.toprettyxml(indent=" ", newl="")[/code] | |
Re: Does this help?[code]>>> import string >>> dd = dict(zip(string.ascii_lowercase, [str(i) for i in range(1,27)])) >>> "".join([dd[s] for s in "abcdejz"]) '123451026' >>> [/code] | |
Re: Excellent snippet vegaseat. The expanded event handler solves a common problem. Here's another working example using the default argument trick:[code=Python]from Tkinter import * from itertools import cycle class SpinLabel(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.pack(fill=BOTH, expand=1) self.master.title("Spin Characters") self.textList = ["/", "--", "\\", "--", "|"] buttonFrame = Frame(self) self.btnList = … | |
| Re: Built-in function round() and string formatting. [code=Python]>>> round(math.pi, 5) 3.1415899999999999 >>> "%0.5f" % math.pi '3.14159' >>> sig_digits = 6 >>> num = 123.456789 >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num) '123.457' >>> num = 1.23456789 >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num) '1.23457' >>> num = 1234567890 >>> "%0.*f" % (sig_digits-len(str(num).split('.')[0]), num) '1234567890' >>> … |
Re: You cannot place a menubar inside a canvas. A menubar goes on the top level window by design. You can place Tkinter widgets on a canvas by using a canvas window object. | |
Re: [I][B]str[/B][/I] method [I]strip()[/I] will remove leading and trailing whitespace characters. If a string only has whitespace characters, [I][B]strip()[/B][/I] will remove them all and it will evaluate False. Example:[code]>>> not 'abcdefg12345 '.strip() False >>> not ' '.strip() True >>>[/code] | |
Re: Following is an example where I already have defined a list of text strings (self.btnList) to be displayed on buttons. When a button is selected (actually, the sequence is ButtonRelease), the button text is assigned to instance attribute "value" and the app is destroyed. In your case, you want to … | |
Re: [code] self.mb["menu"] = self.mb.menu self.mb.menu.add_checkbutton(label="Exit", command=self.quit)[/code]quit() stops the mainloop and destroy() destroys the parent window and it's descendants. | |
Re: Create a Tkinter.Frame to hold the checker board and another frame to hold the buttons. Pack the frames "top". If you want the buttons along the bottom, pack "left". Add internal padding to each frame to get the desired space. To eliminate duplication, initialize a list and iterate on columns … | |
Re: I used this answer on another forum. [quote] Try the following. It may not work on child windows spawned by the process. [code]import subprocess info = subprocess.STARTUPINFO() info.dwFlags = 1 info.wShowWindow = 0 subprocess.Popen("notepad.exe", startupinfo=info)[/code][/quote] | |
Re: cnuzzo, I don't want to rework your code to make it work. Instead, I will give you an example that I think you can apply to your situation in a similar way. I will only create one top level window however. It works like this: [LIST=1] Create the top level … | |
Re: If the data isn't too large, consider storing it in a dictionary. It could work like this:[code]s = '''ID1 ID2 Dist 1 a 50 2 b 20 3 c 10 2 c 100 4 c 80 4 a 70 1 a 90 2 a 34 3 b 5 2 b … | |
Re: Initialize your widget attribute [I]textvariable[/I] with [I]StringVar()[/I]. Use [I]w.set()[/I] to set the value and [I]w.get()[/I] to get the value. | |
Re: I assume variable [I]char[/I] is a series of words. Iterate on [I]chars[/I] as in:[code]for letter in char: if letter.isalpha(): if letter in dict: dict[letter] = dict[letter] + 1 else: dict[letter] = 1[/code] BTW, please use code tags next time. | |
Re: You could use a combination of re and split. Find and remove all items in quotes first then split on space. Example: [code]import re s = '''10471 'AGHADA71' 20.00 2 0.00 0.00 1 1 1.0106 0.312 'AGGTREW 45' 10475 'AGHADA75' 21.00 2 0.00 0.00 1 1 0.9940 -1.217 10810 'AHANE … | |
Re: Perhaps something like this: [code]>>> patt = r'keywords=([^&]+)[&]' >>> re.findall(patt, "keywords=james&keywords=john&keywords=brian&") ['james', 'john', 'brian'] >>> [/code] | |
Re: voolvif, You were not far off.[code]>>> pattern=re.compile(r"\%3D\%27([A-Z]+?)\%27\%3E",re.IGNORECASE) >>> pattern.findall(mystring) ['catherine', 'porter'] >>> [/code] | |
Re: It appears that the first point is constant. Create a list of second point objects, then iterate on the list. [CODE]pointList = [Point(200, 200), Point(150, 200), ............... ] for pt in pointList: Line(Point(100, 100), pt).draw(win)[/CODE] | |
Re: [QUOTE=Gribouillis;1022251]Does anyone know how I can test if an object's type is a builtin type or a user defined type ?[/QUOTE] If it's not builtin, it must be imported from another module.[code=Python]>>> getattr(list, '__module__') '__builtin__' >>> [/code] | |
Re: I am not sure if this is what you want to accomplish, but I HTH:[code=Python]import random def roll_dice(stop=100): # roll five dice and summarize the occurrances of duplicate numbers cnt = 1 dd = {} while cnt <= stop: dice = [random.randrange(1,7) for _ in range(5)] # eliminate duplicates, Python … | |
Re: If you want to determine if a number is prime or not, you should return True (if prime) or False (if not prime). Use the modulo operator to determine if the entered number is divisible by loop variable [I]i[/I]. [code=Python]if not n%i:[/code]If the statement evaluates True, return False. If the … |