2,190 Posted Topics
Re: [CODE]longLength = 0 for i in range(startNum, stopNum + 1): longLength = hailstone(i) if i >= longLength: longLength = i print longLength[/CODE]You are using "i", the incremented value in the for loop and copying it to longLength. Since we don't know what hailstone is, I am assuming it is a … | |
Re: Also, write to the odd numbers file at the same time. Plus, do you want to test for zero or negative integers? [CODE]def write_evens(): even_file = open("evens.txt", "w") odds_file = open("odds.txt", "w") the_file = open_read("integers.txt", "r") for rec in the_file: rec = rec.strip() try : num = int(rec) if num … | |
Re: A simple example using ".get()". The print button calls the getit function when it is pushed. Note the use of "self" to create an object variable that belongs to that class object, so it can be accessed anywhere within the class as well as accessing it as a member of … | |
Re: That's not a good example to copy (although the book ultimately solves that problem if memory serves.). Find one using the canvas widget as it uses a top left corner (x,y) and a bottom right corner. A starting point [url]http://effbot.org/tkinterbook/canvas.htm[/url] [url]http://infohost.nmt.edu/tcc/help/pubs/tkinter/create_rectangle.html[/url] | |
![]() | Re: You would generally use a for loop to iterate through a list. I'm creating one output string, but you could also print within the for loop. Also, please do not use "i", "l" or "o" as single letter variable names as they look too much like numbers. [CODE]test_list1 = [1,2,3] … ![]() |
Re: [QUOTE] but i am interested multi process[/QUOTE] This is an example of multiprocessing/pyprocessing running 2 threads and passing variables back and forth via a dictionary. [CODE]import time from multiprocessing import Process, Manager def test_f(test_d): """ frist process to run exit this process when dictionary's 'QUIT' == True """ test_d['2'] = … | |
Re: Test your code. It doesn't print anything. Also, please use no more than 4 spaces for indents. Finally, it is bad practice to use "i", "l", or "o" as single letter variable names as they can look like numbers. There are at least 3 ways to solve the problem. [CODE]st … | |
Re: [QUOTE]could anyone help me find out the error in my isPrime() function[/QUOTE]You get a return on the first pass through the for() loop. A True if the first divide has a remainder, a False if the first divide has no remainder. You want the True after the for() loop, not … | |
Re: No one has posted this because it is so obvious, and you probably know about it, but to eliminate the obvious...[CODE]class EffectIllusion: second_variable = "second test variable" def __init__(self): self.illusionRace = "***" def change_variable(self): self.illusionRace = "test of variable named illusionRace" class SecondFile: def __init__(self): EI = EffectIllusion() print EI.illusionRace … | |
Re: I've added some print statements to give a hint about what is being processed [CODE]number_of_courses = input ('Number of course to enter... ') for number_of_courses in range (0, number_of_courses): major = raw_input('\nEnter class type ') #cmsc, math, etc.. coursenum = input('Enter course number ') print "input was", major, coursenum print … | |
Re: The simpliest way would be using and IDE, like Idle, that has debugging built in. | |
Re: Your problem is here [CODE] for n in range(num_iterations): print "n before =", n n = B*10 print "n after =", n [/CODE]You use "n" twice and are always multiplying the same number, "B". Something like this should give you the idea of how to go about it. Also, I've … | |
Re: That's fine if you have a nice increment like 0.1, otherwise note that a for loop is just a special case of a while loop. [CODE]r = 0.0 while r < 6.4: print r r += 0.1 print ## ## and if you have an unusual series of numbers, use … | |
Re: [QUOTE]Just remove the spaces between the classes not between CMSC 201[/QUOTE]Since these are all in the handy form of abbreviation-space-number, you can just split and combine them in pairs of two. I would suggest a for loop with a step of two to iterate through the resulting list, and combine … | |
![]() | Re: A dictionary with the letter as the key is the straight forward way to do this. Otherwise use a function with a for loop to pass the letter and the string to the function.[CODE]import string letter_dict = {} for letter in string.ascii_uppercase: letter_dict[letter] = 0 f = [ "abc def\n", … ![]() |
Re: The question is not clear, so let us say if number_list = [1,2,3,4,5,6,7,8,9] 3 = maximum numbers that can be combined (and single numbers are not included), then you would use a function to calculate the sum of a group of numbers and return True or False def return_sum(number_list, compare_to): … | |
Re: Some ideas. First, tkinter does not display all image types. I do not think that it will display a JPG but am not sure. Convert some pictures to a gif and see it that works. Second, "file" is a reserved word and so should not be a variable name, Third, … | |
Re: Use a dictionary to store the class instances. [CODE]class ClassTest : def __init__ (self, desc2="***") : self.field1 = "*" self.field2 = 0 self.field3 = desc2 def chg_field1(self, value): self.field1 = value #========================================================================== class_dict = {} class_dict[1]=ClassTest("First Class") class_dict[2]=ClassTest() for key in class_dict: print "%d %15s, %3d, %s" % (key, class_dict[key].field1, … | |
![]() | Re: Concerning your original code, you declare ind1 and ind2 beneath the if (), so keep them there[CODE]#----------------------------------------------- if user_in in users: users = list(users) passw = list(passw) ind1 = users.index(user_in) ind2 = passw.index(passw_in) users = tuple(users) passw = tuple(passw) ## but if neither user name or password is found, ## … ![]() |
Re: This should be textfile, not textfile.name (a simple "print line" will show that nothing was read). Also, look at the indents. I am assuming that this was a cut and paste error as the interpreter should give an error for the code you posted. Also, textfile.close() doesn't do anything. It … | |
Re: Defining and then calling the functions under the if() statements serves no purpose. Stripped to the minimum, it would be: [CODE]#Convert C to F if x == 1: Tc = input ("What is the Celsius Temperature ? " ) print 'The temperature is %.2f Degrees Fahrenheit' % \ (9.0/5.0 * … | |
Re: Note that you add 2 to numbertotest before you break, so the prime would have been 3001 (don't know if that really is a prime). [CODE]## assume numberofprimesfound = 999 while numberofprimesfound < 1000: . . . elif numbertotest%divisor <> 0: numbertotest = numbertotest +2 numberofprimesfound = numberofprimesfound +1 divisor … | |
Re: [QUOTE]word = raw_input("What word should we convert? ").upper() word.translate(cypher)[/QUOTE]If the first statement, you define 'word' as a string. In the second statement, it appears to be a class with the method "translate". You can not use the same variable name for two different types of memory. | |
Re: For exactness, use the decimal class. My take on the program using decimal. Note that decimal.Decimal is imported as "dec" to avoid confusion with the variable names. Also, I am not a decimal class expert and so use divmod to get the integer and remainder/decimal portions of a number. There … | |
Re: [CODE]for file in open(a): for line in file: if b in line:[/CODE]One problem is here. First, do not use "file" as it is a reserved word. Second, it should be [CODE]for file_name in a: for rec in open(file_name): if b in rec:[/CODE]Third, you could have directories in "a" as well … | |
Re: This is correct, as you are updating x[1], that is y's key is also 1 and so overwrite's whatever is in x for that key. Change to y[2][3]=3 or use x[1].update(y[1]) and you should get what you expect.. If that's not satisfactory then you will have to write your own … | |
Re: Apparently the question is not specific enough. Do you want a numpy array or a multi-dimension list? If you want a multi-dimension list, that can be done with for loops. You can also use a dictionary of lists, using the row number as the key, if that is easier to … | |
![]() | Re: [QUOTE=gianniszaf;985957]Making counts on a months data[/QUOTE] You should be able to process the data file(s) once, i.e. one 5 hour period, and store the counts for all of the customers in a dictionary or SQL file in one pass. Worst case is that you process the data once and output … |
Re: There is an error on this line, no colon for line in objconfigfile.readlines() I would suggest that you print every line because if line == None: will probably never be true, but you will have to print it and see. Also, exec(line) expects "line" to be a valid python statement. | |
Re: Generally, this is called a callback. You have to connect the widget to a slot (function) when a signal happens. See the second example here [url]http://www.commandprompt.com/community/pyqt/x1408[/url] | |
Re: [QUOTE] unable to execute gcc: no such file or directory[/QUOTE]You also have to have libcurl installed. PyCurl is just the Python bindings for libcurl. Apparently, you are installing from source and so have to compile with the C/C++ compiler installed (gcc). Try to find a compiled binary for libcurl and … | |
Re: Te following works fine for me. What version of Python are you using? [CODE]def sum_average(*args): size = len(args) sum1 = sum(args) average = sum1/float(size) # return a tuple of three arguments # args is the tuple we passed to the function return args, sum1, average print sum_average(1, 2, 3, 4, … | |
Re: For future coding conundrums, an "or" can be viewed as an if / elif, and "and" can be viewed as two nested if statements, which you would adapt to while() statements. [CODE]while overwrite != 'Y'.lower() or overwrite != 'N'.lower(): becomes if overwrite != 'Y'.lower(): do something elif overwrite != 'N'.lower(): … ![]() | |
Re: Start with the first one here (effbot) [url]http://www.google.com/search?hl=en&client=opera&rls=en&q=checkbutton+tkinter&aq=f&oq=&aqi=g-s1[/url] | |
Re: Yes, "for line in f:" is buffered, meaning it reads more than one record at a time into a buffer. Use readline instead [CODE]fp=open(fname, 'r') data_in = fp.readline() while data_in: print data_in, ## assumes "\n" in record data_in=fp.readline() [/CODE] | |
Re: I think you are talking about something like this, although it seems a round-about way of doing it.[CODE]class Person: def __init__(self,name): self.name = name class ClubMember: def __init__(self,name): self. new_clubmember = Person(name) CM = ClubMember("Harry") print CM.new_clubmember.name[/CODE] | |
Re: Less exotic [CODE]import random def random_tri(): return int(random.triangular(1,100)) a=random_tri() c=0 while random_tri() != a: c+=1 print(c)[/CODE] | |
Re: The standard anagram solver solution is to sort all of the letters in the words and place them in a dictionary, so to find the anagrams for "dog", you look up "dgo" in the dictionary and, it will give you 'dog' and 'god'. You don't have to mess with all … | |
Re: Threading is not necessary. You can just use a while loop and exit after a certain amount of time or number of guesses.[CODE]class LoginScreen: def __init__(self): self.root = Tk(screenName=None, baseName=None, className='start screen', useTk=1) self.str_1 = StringVar() self.root.title('Login') self.root.geometry('300x200+270+50') self.userlabel = Label(self.root,text='Anvandarnamn') self.user = Entry(self.root) self.passwordlab = Label(self.root,text='losenord') self.password = Entry(self.root, … | |
Re: [QUOTE]I started doing a script that would search for the column headers (ie."datas") and get the next lines (and maybe store these in a dictionary datas {} ... ? but, I'm getting stuck on this[/QUOTE]This is a fairly common use for computers and a fairly common question. [CODE]def process_data(list_in): """ … | |
Re: It appears that you are trying to code one ot the two following examples. Do you want one name, time, etc., or to print all combinations of them?[CODE]##--- print one name, time, and thing ---------------------- def find_text(text_to_find, list_in): if text_to_find in list_in: ## will return only the first item found … | |
Re: You have to install both libxml2 and the python binding for libxml2. You can find download links for both libraries here [url]http://xmlsoft.org/[/url] What distro are you using. Most disto's package managers can install these. [QUOTE]ImportError: /usr/local/lib/python2.5/site-packages/libxml2mod.so: undefined symbol:[/QUOTE]See if the python bindings for libxml2 are in /usr/local/lib/python2.5/site-packages/. They could very … | |
Re: Add a print statement to see what is going on[CODE]from progress import PB from time import sleep def main(): bar = PB(300, 50) bar.open() for i in range(10): print "updating bar by", (10.0 * i) / 100 bar.update((10.0 * i) / 100) sleep(1) bar.close() main() [/CODE] | |
Re: [QUOTE]I'm very new to python,[/QUOTE]Depends entirely on your level of knowledge. If you understand dictionaries, then that would be the easiest to understand. The dictionary key would be the letter, pointing to a list of values. For any letter, list_value[0] would be the first line, list_value[1] the second, etc. So … | |
Re: I really don't find any reason to "dumb it down" for the general population. Agreed, although compared to 15 years ago when I first started, Linux has a much, much wider acceptance and is growing at the expense of other OS's. | |
Re: The only way that I know is to use a container. The list would only contain a reference to the container and so print the current value.[CODE]print "\n", "list test", "-"*50 t_list = [3] omega_list = [0] write_loop = [t_list, omega_list] for j in range(0, len(write_loop)): print write_loop[j][0] print " … | |
Re: Add some print statements to see what is going on [CODE]def load_products(filename): filename = 'C:\Users\User\Desktop\cssetute\products.txt' f = open(filename, 'U') dictionary={} for line in f: line = line.rstrip() list1 = line.strip().split(',') print "list1 created =", list1 tuple_list=[] print "searching list1", list1 for item in list1: print " item =", item if … | |
Re: if ID in product_dict: for starters. values_list = product_dict.values() will give a list of all the values. This [url]http://www.daniweb.com/code/snippet806.html[/url] is an example, but the easiest way is to reverse the original dictionary, unless it is huge. That is, have one dictionary that is key-->value to look up the key, and … | |
Re: This easiest to understand, IMHO, is to create a dictionary with the row & col values as a tuple, for the key pointing to a list of the element number(s). Then, if the list has a length greater than one you have duplicates and can add the corresponding Var1, or … | |
Re: Break the file up into groups by reading the file one line at a time and append to a list. When you hit a "Computer name" record, send the list to a function and process that group of records however you want. Then re-define the list as empty and rinse … |
The End.