- Strength to Increase Rep
- +0
- Strength to Decrease Rep
- -0
- Upvotes Received
- 10
- Posts with Upvotes
- 10
- Upvoting Members
- 7
- Downvotes Received
- 1
- Posts with Downvotes
- 1
- Downvoting Members
- 1
102 Posted Topics
[code=Python]... for char in message: # check if the character is alphabetic if char.isalpha(): ...[/code] Regards, mawe
Hi sneekula, sorry, I totally forgot to answer in the last thread :confused: Ok, your second question is an easy one ;) [php]import Tkinter as tk import tkMessageBox def ask_quit(): if tkMessageBox.askokcancel("Quit", "You want to quit now? *sniff*"): root.destroy() root = tk.Tk() root.protocol("WM_DELETE_WINDOW", ask_quit) root.mainloop()[/php] Ok, now the quit - …
This should do what you want: [code=Python]print "%x" % 255[/code]
[code=Python]a = [4,5,6] s = set(a)[/code] Now s is a set :) For further info have a look at the [url=http://docs.python.org/lib/types-set.html]documentation[/url].
sneekula, I don't want to sound rude, but your one-sentence-question-posts are annoying. Why don't you try to find the answer alone first? If you're stuck, post what you have tried and we will try to help you. BTW: The internet is full of isprime functions in any language you can …
Hi! You have to install idle first (via apt-get or synaptic) ;) The dependency package is called [i]idle[/i], the current one [i]idle-python2.4[/i]. Regards, mawe
Hi! Well, you did it ... nearly :) [code]words = string.split(p)[/code] Ok, [i]words[/i] is now a list, and it's elements are the words in the sentence. You want to know how many words there are in the sentence ... so ... how do you get the number of elements in …
Hi! The first one without any if-elif: [code=Python]def search(l, key): return key in l[/code] For the second one, [b].index()[/b] is your friend ;) [code=Python]def search(l, key): if key in l: return l.index(key) return False[/code] EDIT: Oh sorry, I didn't see it had to be recursive. [code=Python]def search(l, key, index=0): if …
Hi! [code]for i in data: print "#" * i[/code] :) Regards, mawe
Hi! [quote="Python Doku"]__call__( self[, args...]) Called when the instance is ``called'' as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...). [/quote] So ... [code]In [35]: class A: ....: def __init__(self): ....: print "init" ....: def __call__(self): ....: print "call" ....: In …
Hi! [quote="Dark_Omen"] I know this is from awhile back, but you can use print in ruby and it does the same thing as puts. [/quote] Nor really ;). Try this [code]print "not" puts "the" p "same"[/code] Regards, mawe
Hi! Nice! :) I didn't have a detailed look on the code, but *) instead of [code]% (board_list[0], board_list[1], board_list[2], board_list[3], board_list[4], board_list[5], board_list[6], board_list[7], board_list[8]) [/code] it may be easier to write [inlinecode]% (tuple(board_list))[/inlinecode] ;) *) I won several times, but the program didn't notice it. That's frustrating :-| …
The name says it all, I think. You have a nested list, you want to flatten it (e.g. because you want to count the occurence of an element), this function is all you need ;)
liz517, could you please explain what your code [i]should[/i] do.
I'm having trouble unzipping it on Linux. All I get is the error message [i]unsupported compression method 98[/i] :confused:
Another way: [code=Python]def group( lst, n ): ''' splits an iterable in parts with length n, e.g. group( [1,2,3,4], 2 ) ==> [ (1,2), (3,4) ] ''' return zip( *[ lst[i::n] for i in range(n) ] ) aa_dict = { "AAA": "A", "GGG": "C", "CCC": "B" } s = "AAAGGGCCC" …
Hi! Here's a snippet using Tkinter: [code]from Tkinter import * def click(key): if key == "=": # here comes the calculation part pass # but that's up to you ;-) else: display.insert(END, key) root = Tk() keys = ["789/", "456*", "123-", "0.=+"] display = Entry(bg="white") display.grid(row=0,column=0,columnspan=4) x,y = 1,0 for …
Hi! I guess what you are looking for is [b]basename[/b] and [b]dirname[/b]. [code] $ dirname /users4/st/jdoe/prog.c /users4/st/jdoe $ basename /users4/st/jdoe/prog.c prog.c [/code] Regards, mawe
I once wrote one :icon_wink: It's part of one of the greatest periodic table programs of the world *cough*. You can find it [url=http://freshmeat.net/projects/pyperiod/]here[/url]. Then there is [url=http://pyparsing.wikispaces.com/]pyparsing[/url]. You might be interested in one of the examples ... [url=http://pyparsing.wikispaces.com/space/showimage/chemicalFormulas.py]chemicalFormulas.py[/url] :)
700 loc for a simple roulette game? I guess you could shorten it a bit :icon_wink: [code=Python]if spin == 3 or spin == 4 or spin == 5 or spin == 6 or spin == 7 or spin == 8 \ or spin == 9 or spin == 10 or …
I think OCaml is still missing: [code]print_endline "Hello World!";;[/code] Two GUI versions: The first one with the built in Graphics module: [code]Graphics.open_graph " 80x20";; Graphics.moveto 5 5;; Graphics.draw_string "Hello World";; read_line () [/code] And one with Tk: [code]open Tk let top = openTk () let l = Label.create top ~text:"Hello …
Hi, your "error" is a very common source of confusion :) Let's take a slightly simpler example: [code=Python]In [1]: lst = ["a", "b", "b", "d"] In [2]: for elem in lst: ....: print "before: ", lst # check the list ....: print elem, lst.index(elem) # check which element we are …
[quote]But I'm again puzzled by how Tkinter does things. Some examples of code at the beginning say "from Tkinter import *" and some say "import Tkinter as tk" and I've even seen "from Tkinter"...nothing else?[/quote] Let's see how you e.g. call an Entry with these (from Tkinter alone won't work, …
[code=Python]>>> import sys >>>[/code] Hmm, it seems to work. What did you expect to happen? :)
[code=Python]writer = csv.writer(open(("beta_%i.%i.%i.csv" % (now.day, now.month, now.year)), "ab")[/code]
Hi! [code=Python]print "\\".join((str1, str2))[/code] or [code=Python]print "%s\\%s" % (str1, str2)[/code] If you want to make this os-independent, have a look at the [b]os[/b] module ([i]os.path.join[/i] might be your friend :)) Regards, mawe
[i]str()[/i] is your friend. [code=Python]FILE.writelines( str(b) )[/code]
[code=Python]def increase(num, b2, x): if num%(b2**x) < num: return increase(num, b2, x+1) print x return x[/code]
Hi, here's one way (not very clever, but it works): [code=Perl]@numbers = (12, 12, 23, 123, 3, 234, 22333, 223, 3); foreach $number (@numbers) { # fill all numbers with whitespace to a length of 5 $number = sprintf("%-5i", $number); # replace the whitespaces with . $number =~ s/ /./g; …
You don't need a [i]begin[/i] in Python,: [code=Scheme](if (condition) (begin (do-something-1) (do-something-2)) do-something-else)[/code] [code=Python]if condition: do_something_1 do_something_2 else: do_something_else[/code]
[i]place[/i] seems to be a nice and easy way for positioning, but it's not often used. e.g. think of what happens if you want to make some little changes to the GUI. You have to change all coordinates of (mostly) _everything_ you positioned :icon_wink: BTW, you can use a combination …
A little hint: [code=Python]for i in range(1, 5): print i[/code]
Hi, please put your code in code tags, otherwise the indentation is lost :icon_wink: `if number != 4 and number != 5 and number != 6 and number != 8 and number != 9 and number != 10:` This can be written a bit shorter: `if number not in [4,5,6,7,8,9,10]:` …
I don't really understand what you want. You want to run this "game" from Idle _and_ have a Tk-GUI? What should be displayed in the GUI. And why run it from Idle? BTW, do you mean the editor "idle"? I'm just asking because you talk about a "main Idle" and …
You enter a number (say 10), press Enter and the [i]update()[/i] function does the following: - it sets [i]amount[/i] to 0 - reads the number from the Entry (10), adds it to amount (10), sets the label text to amount (10) - reads the number from the Entry (10), adds …
There is a widget called [i]Text[/i], which I think is what you want. An [i]Entry[/i] is always one line high (per definition ;))
Is this from [i]diveintopython[/i]? If so, then the first snippet [url=http://www.diveintopython.org/power_of_introspection/index.html]on this page[/url] is what you need ;)
[quote]I keep getting an error like 'list' object has no attribute 'split'.[/quote] And you really don't know why? [code]words = string.split(s)[/code] Ok, from your other post you know that [i]words[/i] is a list. Fine. [code]ch = string.split(words)[/code] What are you doing here? You want to split a list? Worse than …
Hi! Python usually produces very useful error-messages, like here [quote="Python"]TypeError: argument 1 must be string or read-only character buffer, not float[/quote] Ok, you know what to do, right? Just convert your number to a string ;) [code=Python]a = 123.44 f = open("data1.dat", "w") f.write( str(a) ) # str() converts to …
As scru says, this is not an easy task if you have such a nested structure ;) I once wrote a function which "flattens" these monsters :) [code=Python]def flatten(lst): for elem in lst: if type(elem) in (tuple, list): for i in flatten(elem): yield i else: yield elem[/code] With this, the …
Congratulations for finding the solution by yourself! [code=Python]def main(): import string[/code] This is bad style. Imports should be the first lines in your script. BTW, you don't need the string-module here. Strings have their [i]lower()[/i] method ;) If you are interested, with the builtin [i]sum()[/i] and list comprehension, this can …
Hi! This question is not dumb, in fact it's probably the most frequently asked one on the net :) Maybe these lines will help you (btw, [i]id()[/i] returns the memory address of an object): [code=Python]>>> a = [1,2,3] >>> b = a >>> c = a[:] >>> id(a) -1210949716 >>> …
Untested! [code=Python]lunch_start = time.mktime(time.strptime("11:00-20.02.2007","%H:%M-%d.%m.%Y")) lunch_end = time.mktime(time.strptime("12:30-20.02.2007", "%H:%M-%d.%m.%Y")) now = time.mktime(time.localtime()) if lunch_start < now < lunch_end: print "Hey, it's lunch time!![/code]
Hi! If you do all this in a Canvas, that's very easy: [code=Python]import Tkinter as tk root = tk.Tk() c = tk.Canvas() c.pack() # the image image = PhotoImage(file="whatever.gif") c.create_image(0, 0, image=image) # and now the text at coordinates (50, 50) c.create_text(50, 50, text="whatever", fill="blue") root.mainloop()[/code] Regards, mawe
Yes, there are ways to do this. Here is one: [code=Python]import os def do_y( filename ): print filename for filename in os.listdir("/path/to/dir/x"): do_y( filename )[/code] Regards, mawe
Hi! Well, at least it's an interesting way to do it :) Because Python is an OO language, I would probably have a class [i]Monster[/i] and an instance-variable [i]self.current_direction[/i]. I think this would be [i]cleaner[/i]. Regards, mawe
Hi! This works: [code=Python]text = file("recipe.txt", "r").read() words = [ i.strip(".?!") for i in text.split() ] chemicals = ["chemicalA", "compoundB", "compoundC", "methanol"] recipe = {} for chemical in chemicals: ind = words.index(chemical) recipe[chemical] = [words[ind-2], words[ind-1]] print recipe[/code] DON'T USE IT!! :) It works with your example text, but is …
Hi! [code=Python]import Tkinter as tk root = tk.Tk() listbox = tk.Listbox(root) listbox.pack() for item in ["one", "two", "three", "four"]: listbox.insert(tk.END, item) listbox.selection_set(first=2) root.mainloop()[/code] Regards, mawe
Hi! Change your [i]aggregate[/i] function to: [code=Python] def aggregate(self): #This function is supposed to decrement the time for hours in range(self.hh, -1, -1): for minutes in range(self.mm, -1, -1): for seconds in range(self.ss, -1, -1): time.sleep(1) self.update() # <== ...[/code] @jrcagle: Do you see the [b]Toggle Plain Text[/b] below the …
Hi! Ad 1: Show some code ;) Ad 2: Tkinter isn't responsible for the window decorations, but your window-manager is. So, no, it is not possilbe to change them from Tkinter. Regards, mawe
The End.
mawe