2,190 Posted Topics
Re: Not recursion, but proof of concept only (and done quickly). Returns zero if it is a palindrome. [CODE]def ispalindrome(pal): return sum([0 if x == pal[(ctr+1)*-1] else 1 for ctr, x in enumerate(pal) if ctr < len(pal)/2]) sentence = "Red Roses run no risk, sir, on nurses order." sentence_clean = ''.join(c.lower() … | |
Re: The simpliest way is an easygui or Tkinter/PMW dialog box with one button. However, this is something found on the web long ago. It is probably also possible to do with ncurses.[CODE]import termios, sys, os #import termios, TERMIOS, sys, os TERMIOS = termios def getkey(): fd = sys.stdin.fileno() old = … | |
Re: Only one for loop would be used because you want to access the same relative position in each string. Also, do not use "i" as a variable name as it looks too much like the number 1. [CODE]def cromo(a, b): inc = 0 if len(a) != len(b): print "both strings … | |
Re: You have to iterate through the entire array, whether you write you own or use a built in method. Since you also want the max also, go through the array once, storing any number that is larger than the current maximum, along with it's position. | |
Re: Some print statements should clear up some things. And note that your __add__ function will yield an error but that is something to address after you get this part working. [CODE] class mylist: def __init__(self,l): self.data = l; def __add__(self,l): self.data = self.data + l def __repr__(self): return self.data x … | |
Re: I have no idea which output file you are talking about or which statement writes the header a second time. If you are talking about this segment of the code, [CODE] outfile = open(dst_file, "wb") outfile.write(content) outfile.close() [/CODE] "wb" creates/overwrites to a new file each time the function is called. … | |
Re: Add a little more to the print statements to tell you where you are. [CODE]def prime(n): print "-"*30 print "starting prime with n =", n if n == 0: print("Equals zero, This is not a Prime number") answer = 0 if n == 1: print("Equals one, This is not a … | |
Re: This stripped down version appears to work for me, but it is rewritten because it was too difficult to tell the i, l, and 1's apart, let alone what the variables with nonsense names are supposed to contain. [CODE]import wx class TestFrame( wx.Frame ): def __init__( self ): wx.Frame.__init__ ( … | |
Re: That is a fairly standard menu. An example only partially completed and tested. [CODE]import math import sys def get_number(func_name): try: arg = int(input("Enter number of degrees for function %s: " % func_name)) return arg except: print("Sorry, we can only calculate for integer degrees") sys.exit(1) def sin_funct(): name = "Sine" val … | |
Re: [URL=http://effbot.org/librarybook/zipfile.htm]Effbot's example[/URL] for reading a file into memory, which can then be written anywhere you like as long as the files are not huge. [CODE]import zipfile file = zipfile.ZipFile("samples/sample.zip", "r") for name in file.namelist(): data = file.read(name) print name, len(data), repr(data[:10]) [/CODE] | |
Re: There is no reason to use a list, or if you do, don't create a new dictionary on every pass through the loop. [CODE]dict1={} n=input("Enter the number of contacts : ") for i in range(0,n): name1=raw_input("Enter your name: ") num=input("Enter your phone number: ") if name not in dict1: dict1[name] … | |
Re: The wiki at python.org is always a good place to start any search [URL]http://wiki.python.org/moin/WebFrameworks[/URL]. | |
Re: Python does not have a keypress capture so you have to use one provided by the system like termios. [CODE]import termios, sys, os TERMIOS = termios def getkey(): fd = sys.stdin.fileno() old = termios.tcgetattr(fd) new = termios.tcgetattr(fd) new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO new[6][TERMIOS.VMIN] = 1 new[6][TERMIOS.VTIME] = 0 … | |
Re: [QUOTE]In: 07:34 08:55 10 20 08:25 09:00 10 20 Out: 08:25 09:00 10 20 07:34 08:55 10 20 [/QUOTE]For a reverse order sort like this, you simply compare for greater instead of lesser, which Python sorts will do with the reverse=True parameter set. Do you also want to take into … | |
Re: Draw it out first, then program each part individually and then put them together. To get you started, here is part of the right-most picture. Continue with a separate function to draw the other half (columns side), and then look at a way to combine them. It you can not … | |
Re: Try [URL=http://wwwsearch.sourceforge.net/mechanize]mechanize[/url] and see if it will work for you. [URL=http://stockrt.github.com/p/emulating-a-browser-in-python-with-mechanize/]An example[/URL]. | |
Re: If you want to print from the word to the end of the line, use index. This untested code will tell you if a word is found. You can then use .join to print out what you want.[CODE]for line in logfile: words_list = line.split(): for word in KEYWORDS: ## or … | |
Re: If you store the prefixes in a list: [CODE] prefix1=userInput1[:3] prefix2=userInput2[:3] prefix3=userInput3[:3] ## ## becomes prefix_list = [prefix[:3] for prefix in userInput] [/CODE]You can then clean up the if() statements: [CODE] if prefix1==networkList[0] or prefix2==networkList[0] or prefix3==networkList[0]: msg+= "\n"+" vodafone ""\t" # # + the other if() statements becomes message_tup … | |
Re: To start you off, look for redundant statements, like all of the if/elif at the end of the program that basically do the same thing. They can be replaced by a simple dictionary of values. [CODE]## The following 3 lines can be deleted for this example #u = 1 #optimism … | |
Re: It does not appear that you are using physical dimensions, since there are no numbers < 11, but this will print 5 random combinations. [CODE]import random shapes = ['square', 'triangle', 'circle', 'left'] physical = range(1, 11) angles = range(10, 91, 10) print " shape phy angle" for j in range(5): … | |
Re: This is a python forum. There are several forums on the open office site. | |
Re: This is the last time I am explaining technique. Start out with something specific that works and expand to the general. Take this code which shows one specific set of 3, and then a second set, and expand so it will print one entire row, then expand so it will … | |
Re: I'm not sure about the question is, but try this code and see if it clarifies things.[CODE]name = 'Jack' print '"name" is this memory address', id(name) def say_hello(): print ('Hello ' + name + '!') def change_name(new_name): name = new_name print "name is now", name print '"name" is this memory … | |
Re: First, add some print statements for testing: [CODE]# add a colomn of 0 >>>for j in range(len(my_list)): row = my_list[j] row.append(0) print "row now =", j, row # Add a row of 0 >>>for i in range(len(my_list)): colomn=my_list[i] colomn.append(0) print "column now =", i, colomn # # a matrix (list … | |
Re: It would be something along these lines, see [URL=http://hetland.org/writing/instant-python.html]"Functions" here[/URL]: [CODE]def CoM_positions(value_in): if some_condition: ## or whatever CoM_return = CoM(value_in) print CoM_return, value_in return CoM_return return_value = find_positions(positions) better_values= CoM_positions(return_value) print better_values [/CODE] | |
Re: There are two problems that I can see. It should be "root.mainloop" and there is no config statement for load menu so only the empty "menubar" is displayed. Also, note the inconsistent use of master and self.master. I did not test with any of the commented lines included. If they … | |
Re: It looks like you have 13 lines of normal text, by which I mean that you could open it normally and read the first 13 lines normally, and count the bytes to skip. Then close the file, open as binary and skip the number of bytes that contained text, but … | |
Re: There is an example in the turtle graphics demo of the Python docs that come with Python. | |
Re: It appears the the backslash is being escaped by Python. Try repr(the_string) as output and see if it helps. | |
Re: Tab is not necessary. Try this code, which will accept anything, letters, numbers, equal signs, etc. [CODE]ltr_str = raw_input("Enter 10 letters ") while len(ltr_str) != 10: ltr_str = raw_input("Enter 10 letters and 10 letters only ") printed = 0 for ltr in ltr_str: ## access each individually print ltr, printed … | |
Re: [QUOTE]i[I] made a start but i cant make it work with the colour nor adding size dimensions.[/QUOTE]That is a little vague. Post one function with a description of what you want it to do. Also in getWidth() and getHeight() it will error out in Python 2.X if a non-digit is … | |
Re: Using x,y coordinates confuses things IMHO. You can just number each square and enter one number for that square. This also simplifies keeping track of moves as you can use a single list with one element for each square on the board. | |
Re: You don't need recursion for this. Are you sure you are not supposed to sort in reverse order, i.e. maximum value is first. You can think of recursion as a while loop, with a function calling itself instead of looping. | |
Re: Three more links: [url]http://hetland.org/writing/instant-python.html[/url] [url]http://python-eggs.org/[/url] [url]http://www.doughellmann.com/PyMOTW/[/url] (more of a reference) | |
Re: Hundreds of records is not much in today's world so you can read each file into a dictionary and go from there. A simple example to associate the two files because I am too tired to do more today. You can omit some of the unnecessary records from the dictionary … | |
Re: I'm not sure what you are asking, but see if this helps: [CODE]## last line under main() print "points for one", point_one.x, point_one.y print "points for two", point_two.x, point_two.y # # and add this additional function to Triangle and call it def print_points(self): print self.A.x, self.A.y print self.B.x, self.B.y print … | |
Re: If you are using Python3.X, then input will return a string which is always greater than 100, print type(score) to see which class it belongs to. | |
Re: Please don't post 54 lines of untested crap here and ask up to clean up your mess. Test each function individually. For example: What happens when you enter 2 students here: [CODE]def getNumber(number): number = input('Enter a number between 2 and 30: ') while number <=2 or number >=30: print'You … | |
Re: You don't have any button or other widget variable "Quit". You possibly want to store the memory address for the buttons in a list and then use button_list[3].clicked() as it is the last/fourth button created. Using the local symbol table is somewhat obfuscated code IMHO. | |
Re: You can do that in two ways, the second way is usually preferred because everything is contained in the class: [CODE]import random class Kortlek(): def __init__(self,colors=["blue ", "yellow ", "red "], \ forms=["triangel", "circle ", "square "], \ numbers=[1, 2, 3], grades=[1, 2, 3]): self.cardList = [] self.playerCardList = [] … | |
Re: You also should test for length before you draw cards: [CODE]def cards(self): if len(self.cardList) > 5): for a in random.sample(self.cardList, 6): [/CODE] | |
Re: [QUOTE]Hey, I've got an array full of random numbers[/QUOTE]It depends on how many levels are in the array. Generally, you would flatten the array into a single level list, and operate on that. If you want to keep the original structure, then you have to test for the type. If … | |
Re: The code works fine for me on Slackware Linux. What are you using for input, and what output do you get? | |
Re: The advantages of Multiprocessing are: it does not use a GIL and it can use multiple cores if you are on a multi-core machine. This means that it is usually faster, but it ultimately comes down to what you understand and can use. | |
Re: It appears the the zero is being read as minutes and the one as the hour. I doubt any of the cron programmers allowed for someone entering "01" instead of just "1". | |
Re: Generally you begin with the input [QUOTE]The user will enter a molecule with each atom individually (CO2 would be entered as): C O O...The user should continue to be asked for more molecules until they enter a blank line.[/QUOTE]Next would be the dictionary you spoke of, sign-->mass, and looking up … | |
Re: Start by using [URL=http://www.google.com/search?client=opera&rls=en&q=python+turtle+triangle&sourceid=opera&ie=utf-8&oe=utf-8]a turtle triangle[/URL] for the tree, add a small rectangle for the tree stump, and then modify the triangle to look more tree like. | |
Re: Note also that you start with zero, not one, and your zero replaces 21, and your 21 replaces 16. One problem is that you generate 26 numbers, zero through 25, with only 25 spaces to hold them. I have no other advice because "And here is what the square is … | |
Re: It depends somewhat on what widget you want to set, but generally you can use configure. This is something I have lying around from somewhere and added a configure to change the background and foreground of the button when the top button is pressed. Another example is [URL=http://www.ferg.org/thinking_in_tkinter/tt080_py.txt]here[/URL]. You also … | |
Re: There have been several examples on Daniweb alone [url]http://www.daniweb.com/forums/thread246963.html[/url] |
The End.