2,190 Posted Topics
Re: Your last 2 lines would be modified thusly[CODE]primeNum = int(raw_input("Enter number here:")) prime_list = listfactors(primeNum) ## your function returns a list for prime in prime_list: print "prime =", prime[/CODE]Also, a second for using modulo or divmod. Your function compares integers and floats and you will be comparing numbers like int=1, … | |
Re: [QUOTE] How would I get it to read the 'a' again?[/QUOTE]You would use the string.find() function. It returns the location of the string if found, or -1 if not found, and can take a second, optional argument that is the starting position for the search. So you would add one … | |
Re: It appears that you are using "text" twice.[CODE]##--- here "text" is the list of records from the file text = file.readlines() file.close() keyword = re.compile("searchword") ##--- here you are reading from the list of records which is named "text" for line in text: result = keyword.search (line) if result: ##--- … | |
Re: Or print every 3rd element and then print the length of the list. | |
Re: [CODE] while n: num = n % 10 squares = num*num n = n / 10[/CODE]"while n:" evaluates to True/False. If n==0, then it is False, otherwise True (for both positive and negative numbers). So you would have an infinite loop, except for the fact that that you are using … | |
Re: [CODE]data=line.split() maxval=data[0][/CODE]because you are using strings instead of integers. So converting to an integer would look like this.[CODE]file_in = open( 'cont.dat',"r") data_in=file_in.readlines() file_in.close() coloumn=[] for line in data_in: data=line.split() ncoverage=len(data) ## convert to integer maxval=int(data[0]) maxid=0 for j in xrange(0,ncoverage,1): ## convert to integer if int(data[j])>= maxval: maxval=int(data[j]) print maxval[/CODE] | |
Re: The error is in this line print (edgelist1[i][1],edgelist1[nextrow][1]) when you get to the last record, nextrow points beyond the end, so you want to use for i in range (0,listmax-1): since there is nothing to compare the last record to. Alternatively, you can use for i in range (1, listmax): … | |
Re: Read the directory and then use os.stat to get file info. You will probably want to use one of st_atime, st_mtime, or st_ctime depending on which OS system you are on. | |
Re: If your OS has some kind of socket support to communicate between processes then you can use that. You could also do something like letting an SQL file take care of the locking issues, and have one or both programs write to file and the other check periodically for updates, … | |
Re: Generally you use the table widget. I don't use Tkinter much but try tablelist [url]http://www.nemethi.de/[/url] and the tablelistwrapper for Python [url]http://tkinter.unpythonic.net/wiki/TableListWrapperpython[/url] You pass it the headings, and then the data as a list of comma separated values. | |
Re: [QUOTE]the moment the thread is killed, a sys.exit() is sent to main thread and the whole program exited. I am still trying to figure out how I can avoid that.[/QUOTE]You can use multiprocessing for Python versions > 2.5, but you have to install it for 2.5 and below (named pyprocessing) … | |
Re: This type of thing generally used for creating a menu using a dictionary. An example of a menu using eval() to call the function:[code]def func_1(): print " func_1 test" def func_2(): print " func_2 test" def func_3(): print " func_3 test" def func_4(): print " func_4 test" ##---------------------------------------------------------------- ## dictionary … | |
Re: [QUOTE]delimited by "\t", "," or " ". If it is any of these, I want to keep the line[/QUOTE]Just test for any one in a line.[code]if ("\t" in line) \ or ("," in line) \ or " " in line: print "keep this line", line[/code] | |
Re: The problem may be in the code below. In the first case, if user_input == '', you are passing a file name with the corresponding directory(s). In the second case, "else", you are passing the directory only, that was input by the user. This type of problem is generally a … | |
![]() | Re: [QUOTE]when I try to remove a single appearance of a number from a square, the number is removed from all the squares in the grid[/QUOTE]Generally that means that you are not declaring a list of independent lists but a list of references to the same list. Show us how you … ![]() |
Re: A quick look says that `classes/__init__()` requires parens, i.e. g.QuitButtonBB() If that does not solve the problem, then post back. And "(code = python)" evidently does not work. Use the plain old "(code)" instead. | |
Re: [QUOTE]Having one list like L1 = ['a', 'b', 'c' ...] need to do pop operation and assign value like, z1 = L1.pop() z2 = L2.pop() z3 = L3.pop()[/QUOTE]In Python you do not have to reassign the value. Use the list values, unless you reassign for some other reason.[CODE]list_1 = ['a', … | |
Re: And the following works fine for me, so it may have something to do with the regex as stated above. Try running the code with the "re.sub" line commented and see if that makes a difference.[CODE]s = u'La Pe\xf1a' print s.encode('latin-1') x = u"Rash\xf4mon" print x.encode('iso-8859-1') ##-----prints----- La Peña Rashômon[/CODE] | |
Re: [QUOTE]by putting the cursor to the end of the for c in chartNames: and pressing an enter I get compile error.[/QUOTE]"I get a compile error" is too vague for anyone to figure out. It is probably that the blank line is not indented at the same level as the other … | |
Re: And if you want to follow along with your first idea, you have to return the total if you want to access it outside the function.[CODE]def test(l): total = 0 number = input("Number: ") for count in range(1, number+1): value = (count) * 20 print "value =", value total += … | |
Re: [QUOTE]Only, you'd have to gpl your code[/QUOTE]This is not true. You can GPL your code if you want but it is not a requirement. Your code automatically comes under the GPL only when it modifies a program that is already GPL'd. You would have to provide a link to the … | |
Re: [QUOTE]EDIT: it's not the print statement that is failing. Sometimes the except statement runs for no apparent reason even after the self.cursor.execute completes[/QUOTE]Which means it is the print statement that is causing the error. Break it up to see which part is the problem (which should make it obvious since … | |
![]() | Re: A google will yield a lot of hits. The python wiki is always a good place to start. [url]http://wiki.python.org/moin/GuiProgramming[/url] |
Re: Because of the slash it gets a little tricky. I have found using split to be more reliable.[CODE]name_list = ['adam', 'bill', 'jon/juan'] list_2 = [] for this_name in name_list: substrs = this_name.split("/") each_name = "".join(substrs) print each_name list_2.append(each_name) print "list_2 =", list_2 # ##-------------- or ------------------------------ list_3 = [] for … | |
Re: You use a StringVar and include the function in the class. Here is a simple example. Note that the same StringVar is used for the second label and the entry box. Take a look at this page [url]http://infohost.nmt.edu/tcc/help/pubs/tkinter/control-variables.html[/url] [CODE]import Tkinter class EntryTest: def __init__(self): self.top = Tkinter.Tk() self.str_1 = Tkinter.StringVar() … | |
Re: PostGreSQL claims to work with 3.0 but I haven't tried it [url]http://python.projects.postgresql.org/[/url] SqLite is good up to 100,000 to 250,000 records depending on size of record and database use. | |
Re: You can do this two different ways.[CODE]items = {'sword':['sword', 3, 15], 'axe':['axe', 5, 25], 'bow and arrow':['bow and arrow', 4, 20]} ##---- the easiest to understand for key in items: print "key =", key weapon_list = items[key] print "\tweapon_list =", weapon_list print "\tprice =", weapon_list[1] print "\tdamage =", weapon_list[2] ##--- … | |
Re: [QUOTE]6.1230317691118863e-17[/QUOTE]Note the e-17. This is a very small number and the closest that floating point numbers can come (similiar to converting 1/3 to a base 10 decimal). See here [url]http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate[/url] and [url]http://www.lahey.com/float.htm[/url] You have to use sloppy logic with floating point numbers [code]print "%10.7f" % ( math.cos(90*math.pi/180)) or x = … | |
Re: Section 1 in this online book has some beginning class examples. [url]http://www.mindview.net/Books/TIPython[/url] | |
Re: What does it return, an error message? exec() will work on things like print statements or 1+2-3, but if you call a function or class then you must already have that function defined, otherwise an error message. Perhaps if you explained what you are trying to do, there would be … | |
Re: See this thread. [url]http://www.daniweb.com/forums/thread198636.html[/url] | |
Re: To start, list_of_numbers is not a list of numbers but an integer. Or, list_of_numbers = int (list_of_numbers) will throw an error because you can not convert a list, and there would be no need for a while loop either. Most people who see list_of_numbers = (input("Enter a list of numbers, … | |
Re: [QUOTE=shoemoodoshaloo;895011]Well, I've defined a new method which parses the data a bit better. I think this may be the solution: ef output(line): '''''' for value in line: outstring = str(line[0]) + " " + str(line[1]) + " " + str(line[2]) + " " + str(line[3]) Data seems formatted better[/QUOTE].join will … | |
Re: effbot has some nice ultra noob stuff for the beginner. See here (and you might want to check out the rest of their tutorial and then continue on with whatever on you have now) [url]http://effbot.org/pyfaq/how-do-i-run-a-python-program-under-windows.htm[/url] Edit: I just realized that this tutorial is for version 2.5 and lower, so it … | |
Re: Split the string on whitespace and check the first character of each word. You can use >= "A", <="Z"; or check that the ord of the character is within the capital letter range; or create a list of capital letters and use "if the first letter is in the list". | |
Re: A third option, if you want to keep the extension.[code]old_file_name = 'bc13abc2.txt' ## This assumes there is only one "." in the file name substrs = old_file_name.split(".") print substrs ## then use siddhant3s' example to find the digits, or some way ## using isdigit() and add substrs[1] (the ending) to … | |
Re: The problem is the @staticmethod decorator in both of your examples. Delete that line and the first program runs OK [plus you only supply 'instance' once, i.e. instance.do_stuff_with_var() ]. I have not tested the second program, but that would be the place to start. Since you only have one "do_stuff_with_variables" … | |
Re: [QUOTE]0.0 -0.9 0.3 -0.4 0.6 -1.0 0.9 0.3 These colums always appear on the same line number. What I want to do is to get a list of all column1row1:column2row1 of all files, then a list of all column1row2:column2row2 and so on[/QUOTE]One way is to append to a list. So … | |
Re: [QUOTE]Somestruct = Struct .new(:character, :characterArray) structure = Somestruct.new(" ", [])[/QUOTE]This is just a simple list or tuple in python. Try this[CODE]structure = [] Somestruct = (" ", []) structure.append(Somestruct) Somestruct = ("1", ["abc", "def"]) structure.append(Somestruct) print structure[/CODE]If you want more than that, then use a list of classes, with each … | |
Re: [QUOTE]traverse(tree.rest)[/QUOTE]tree would have to be an instance of the Tree class for this to work.[CODE]class Tree: def __init__(self, f, r): self.first = f self.rest = r tree = Tree(1, []) vertex = tree.rest print "tree", if len(vertex) > 0: print "Greater" else: print "Lesser/Equal" tree_2 = Tree(1, [123]) vertex = … | |
Re: Use a dictionary, with port number as the key, pointing to a list of servers running on that port.[CODE]allports = list(set(total.keys())) lines = total.values() data_dict = {} for port in allports: for line in lines: this_port = line[1] ## add key to dictionary if it doesn't exist, ## pointing to … | |
Re: If I understand what you want, you want to use if ";" in line: if not "X" in line: etc. | |
Re: [QUOTE]File "F:\My Projects\Python\PhoneBook.py\mainfile.pyw", line 79, in OnPopulate index = self.screen.InsertStringItem(sys.maxint, data[0]) [(1, u'Steve', u'Giddy', u'0654743553'), (2, u'Majia', u'Ukonovic', u'65473032'), (3, u'Joseph', u'Ryanc', u'7663991109')][/QUOTE]If this list is "data" then data[0] is a tuple. If the first tuple is "data" then data[0] is an integer. Print data and type(data). | |
Re: Most newer BIOS have a password option that will require a password to start the computer. Obviously, if you use this you do not want to loose the password, otherwise you will have to flash the BIOS or jump through some other hoops to be able to do anything at … | |
Re: Add 2 other print statements to see what is happening. Note that the statement x=[] does nothing since you redefine x in the next statement.[CODE]n=range(0,31) x=[] for x in n: AL=5000.00 MM=[] print "start of loop", MM MM_Income=12000.00 MM_Cost=8000.00 MM_Inc=800.00 MM_Own=11 MM_ROI=(MM_Income/(MM_Cost+(MM_Inc*x)+AL)*100) MM.append(MM_ROI) print "after appending", MM print (MM_ROI) MML=zip(n,MM) … | |
Re: You don't use the "i" from the for loop so "A" and "B" will always be the same for all 10 iterations through the loop. Also, try running this with these print statements added, which show that the two variables named "i" are different blocks of memory, and that "A" … | |
Re: You want to stop at the next to last item in the list, so "+1" will be the last item. Also, do not use "i", "l", or "o" as single letter variable names. They look too much like numbers. [code]list1 = [4,20,2,19,3254,234,21,03] stop = len(list1) - 1 low = list1[0] … | |
Re: [CODE] if row[i] == '': v = 0 else: v= float(row[i]) self.csvData[i].append(v)[/CODE]In this code, it could be the append statement that is taking the time since it is a large file. Store "i" and "v" in a dictionary until the end. After the loop is finished, instead of appending, it … | |
Re: Print dirList1 and dirList2 at the beginning of the function, or at least the length of each. The first step is to make sure that both contain what you think they contain. | |
Re: [sarcastic] Damn, how many gigabytes is this code. Trim it down to one million lines of code so you don't have to zip it. [/sarcastic] |
The End.