2,190 Posted Topics
Re: [QUOTE]what do these two strings have to do with the A & c in RANKS & SUITS?[/QUOTE]One is a class instance and one is a class attribute. See here [url]http://diveintopython.org/object_oriented_framework/class_attributes.html[/url] | |
Re: You have to enter "d:\" to make it equal to "d:\\" since backslash is the escape character. Why not make it simple and test for "D:" only. I've added some print statements to clarify. [CODE]#!/usr/bin/python3 import os import sys whileloop = True while(whileloop): initial_drive = "C:\\" inputline = input(initial_drive) print("inputline … | |
Re: [QUOTE] i want to receive messages from the threads running in other python modules and display or process in my pyqt application.[/QUOTE]You would have to communicate between the threads. I use multiprocessing instead of threading and so would communicate through a manager list or dictionary. This is an example of … | |
Re: One thing, you can not open a directory, so you should test that it is a file. [CODE]import os a = [] a = os.listdir('.') for t in a: if os.path.isfile(t): f = open(t,'r') print t, "opened" f.close() else: print " ", t, "not a file" [/CODE] Also, if you … | |
Re: The same code with a call back to show which button was pressed. Hopefully you will use a class structure for this as that eliminates shared variable problems. [CODE]from Tkinter import* def cb_handler(event, cb_number ): print "cb_handler - button number pressed =", cb_number root = Tk() frame = Frame(root) frames … | |
Re: [CODE] if option==3: active<>"on"[/CODE]This will print "True" if active != 'on' and print "False" otherwise. <> has been deprecated so use "!=" instead. I think you want [CODE] if option==3: active = "off"[/CODE]Also you can simplify the following code. [CODE] if option<>1 or option<>2 or option<>3: ondamenu() ## ## can … | |
Re: I don't use regex as a rule and it is not necessary in this case. [CODE]found = False fp = open(fname, 'r') for num, line in enumerate(fp): line = line.strip() if line.startswith(search_term): found = True if found: print num, line fp.close() [/CODE] [QUOTE]and a number of lines before and after … | |
![]() | Re: First you want to check that the word ends up as an even number, otherwise you can't divide it in two. Tiger = 59 so is not a good choice. [CODE]import string def number_equiv(word): ## I prefer a list over string.find (so sue me) letters_list = [ ltr for ltr … ![]() |
Re: It's personal preference, but I would use a dictionary in the players() function. [CODE] def player(self, choice, Com): win_d = { 'rock':'Scissors', 'paper':'Rock', 'scissors':'paper'} if choice.lower() == Com.lower(): return 'Tie!' elif win_d[choice] == Com: return 'You won!' else: return 'You lost!' [/CODE] Also, you can create all four buttons with … | |
Re: This program should use a class. For the foo_sell() function as it is, pass 'cash' and 'dealers' to it and return them as well, avoiding globals. [QUOTE]I have never had a problem with declaring globals like this[/QUOTE]You have now and hopefully have learned that lesson. | |
Re: This is why we have SQL. I have a work in progress that generates simple SQLite code for throw away applications. This is the code it generated for your application. The test_data() function adds some records with the data you provided. You should be able to figure how to add … | |
Re: You want to add buttons for the functions to add, multiply, etc and tie that to the proper event. Right now, every button you push calls buttonNPush which prints the button. You also have to issue a get() to retrieve data. See this link for an example. [url]http://www.uselesspython.com/calculator.py[/url] | |
Re: 'student' has not been declared so who knows what this does. [CODE] data = student.sport k = len(student.fname)[/CODE] What happens when the key is not found? [code] for i in data: freq[i] = freq.get(i, 0) + 1 [/CODE] Your code is poor at best. Add some print statements, print the … | |
Re: It should be something with ButtonReleaseMask. Try this code and Google for ButtonReleaseMask if it doesn't work. [CODE]import Xlib import Xlib.display display = Xlib.display.Display(':0') root = display.screen().root root.change_attributes(event_mask= Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask) while True: event = root.display.next_event() print "Hello button press" [/CODE] | |
Re: FWIW, a more generic solution using a list to keep track of the maximum's value and the number of times it occurs. [CODE]def find_the_max(blist): ## a list containing the value and the number of times it appears max_group = [blist[0], 0] this_group = [blist[0], 0] for el in blist: if … | |
Re: effbot's tutorial turns up as the first hit for a Google of "tkinter scrollbars". Since we don't know what you want to attach it to, effbot would be the place to start. This is effbot's code for a canvas widget as an example. [CODE]from Tkinter import * root = Tk() … | |
Re: This is a decent tutorial on functions [url]http://www.penzilla.net/tutorials/python/functions/[/url] | |
Re: I had a simple solution using a list of lists to associate the letter with the original position. [CODE]def find_letter(letter, list_in, stars_list): for ch_list in list_in: ##print(" comparing %s and %s" % (letter, ch_list[0])) if letter == ch_list[0]: print(" found %s" % (ch)) ## replace '*' with letter el = … | |
Re: It would generally be something in this ballpark [CODE]file_pointer = open("Handylist.csv", "r") name_in = raw_input("Enter the name") for rec in file_pointer: substrs = rec.split() name = substrs[x] ## don't know where this is if name == name_in: print("%s Found" % (name)) [/CODE] | |
Re: It's pretty straight forward, and since I'm sick and not doing much today... [CODE]test_data = [ "c The random seed used to shuffle this instance was seed=1755086696", "p cnf 1200 4919", "-35 491 -1180 0", "479 1074 -949 0", "609 1177 -67 0" ] formula = [] for rec in … | |
Re: [QUOTE]main.cpp:1:20: error: Python.h: No such file or directory[/QUOTE]Python.h is under /usr/include/pythonx.y on my Slackware box. If you don't have it then you will have to install the source. | |
Re: This statement will never be true elif (data[x]>57 and data[x]<48): | |
Re: For timed events you generally use the "after" function. [CODE]from Tkinter import * import datetime import time class ChangeTime(Frame): def __init__(self, master=None): master.geometry("100x50+5+5") Frame.__init__(self, master) self.pack() self.timestr = StringVar() lbl = Label(master, textvariable=self.timestr) lbl.pack() # register callback self.listenID = self.after(1000, self.newtime) def newtime(self): timenow = datetime.datetime.now() self.timestr.set("%d:%02d:%02d" % \ (timenow.hour, … | |
Re: [QUOTE]I have absolutely no clue how to do it with a list of lists[/QUOTE] [CODE]test_matrix = [ ["A", "b", "c"], \ ["R", "S", "s"], \ ["X", "Y", "Z"] ] for sublist in test_matrix: print sublist for ch in sublist: if ch == "S": print "\nS found in", sublist, "\n" [/CODE] | |
Re: You can put the redundant code into a function [CODE]def get_two_numbers(): while True: try: x = float(raw_input("number one\n")) break except ValueError: print "Wrong input" while True: try: y = float(raw_input("number two\n")) break except ValueError: print "wrong input" return x, y def add(): x, y = get_two_numbers() print 'your answer is:', … | |
Re: getRandEntry() calls startLink(theRandoms) which calls getRandEntry() which will create and infinite loop. | |
Re: This will change every 2nd letter to "x" and every 3rd letter to "y", but note that the 6th letter could become either "x" or "y" depending on the logic. [CODE]def change_chr(chr, x): if not x % 3: return "y" if not x % 2: return "x" return chr mystring … | |
Re: [QUOTE]fetching data from qt[/QUOTE]The short answer is to look at QLineEdit here [url]http://www.devshed.com/c/a/Python/PyQT-Input-Widgets/[/url] More tutorials are on the wiki [url]http://www.diotavelli.net/PyQtWiki/Tutorials[/url] | |
Re: There are many possible problems with this code, one of which is the following. Add some print statements to see what the program is doing. [CODE] def __radd__(self, other): print "__radd__ return =", self, other, type(self), type(other) return self + other def __rtruediv__(self, other): ## add a similar print statement … | |
Re: You draw an object like a circle and fill it with the color selected inside a try/except. It will fail if it is not a valid color. I think Tkinter uses an r,g,b tuple so your program should accept that as well as a name. | |
Re: input is raw_input in 2.6 That's the only big difference along with Tkinter becoming tkinter. For print you can use either print str print(str) and string.split(a) has been deprecated for while for all versions of Python, so use a.split() in all versions as well. There should be very few problems … | |
Re: Numpy will probably already do everything you want [url]http://www.scipy.org/Tentative_NumPy_Tutorial#head-926a6c9b68b752eed8a330636c41829e6358b1d3[/url] | |
Re: You can use a dictionary as a counter, but you first have to convert the key to a tuple. [CODE]arr=[[10, 2, 10], [10, 9, 10], [10, 9, 10], [10, 9, 10], [10, 9, 10], [22, 9, 10], [10, 9, 10], [10, 9, 10], [22, 9, 10], [10, 9, 10]] unique_dict … | |
Re: [QUOTE]but import statement imports it only once and i have no clues how to fix it.[/QUOTE] Why do want to import the same thing a second time? You already have it in memory. Putting the same thing in another block of memory is a waste. To test for a number, … | |
Re: First, try running this code in Idle. It will tell you that there are problems with this statement with a variable name that contains special characters. Starting_weight_of_food_in_lbs{:}ozs=input("Enter Starting_weight_of_food_in_lbs{:}ozs") Take a look here for getting started with Idle [url]http://www.ai.uga.edu/mc/idle/index.html[/url] ![]() | |
Re: +1 for "Dive Into Python" and "Think Like A Computer Scientist" For the non-programmer, something like this might be better as it explains the logic behind things like while() loops, etc. [url]http://www.pasteur.fr/formation/infobio/python/[/url] | |
Re: Doug Hellmann always has good explanations. [url]http://www.doughellmann.com/PyMOTW/threading/index.html[/url] [url]http://www.doughellmann.com/PyMOTW/multiprocessing/index.html[/url] | |
Re: [QUOTE]File "./ste.py", line 64, in disconnect self._conn.close() _mysql_exceptions.ProgrammingError: closing a closed connection[/QUOTE]Why are you closing a connection (twice) for a db that you are adding records to. How about some code. Also, you do not have to commit after every add, so wait until after you have added x records, … | |
Re: > the first phase will read in the file and find all of the tags and save them into a queue Wouldn't that just be a list or a dictionary. > the second phase will take in the queue of tags from the first phase and check to see if … | |
Re: The first two posts found in a search of this forum, after your post which is the first/latest, have working solutions that you should be able to adapt. | |
Re: [QUOTE]The problem I have is after I did the rstrip(), nothing really happened to the text file? I don't really know what I'm missing.[/QUOTE]You have to write the data to a file. You read the disk file into memory, change it in memory and then exit the program. If you … | |
Re: I would suggest two classes, one that handles the physical details of the SQL database and one for everything else. So, if you want to modify a record, the second class would generate a GUI to ask for info on the record. The info would be passed to the SQL … | |
Re: [QUOTE]matrix[x][y] = corr(file[x], file[y]) TypeError: 'list' object is not callable corr = ['0.1', '0.2', '0.3', '0.4', '0.5', '0.6'] ## list matrix[x][y] = corr(file[x], file[y]) ## function [/QUOTE] You are using corr as both a list and a function. You'll have to rename one of them. | |
Re: Start by asking the player(s) for a name and store it in a dictionary that will also keep track of their score. Then, open one graphic window that will simply display the name and score of the player(s). Worry about the multiple windows and clicking on each at a latter … | |
Re: [CODE]if clickx>=iris1x and clickx <=iris2x and clicky>=iris1y and clicky<=iris2y:[/CODE] If you are dealing with eyes, etc. that are circles then your test doesn't work. I think you would have to 1. take the distance of the mouse-click x-coordinate from the center of the circle 2. make sure the difference is … | |
Re: If you can call/execute external programs, then you can put each routine in a function in a separate file with the appropriate return value. Otherwise, you could do something like the following to eliminate the else statements, and hopefully the program is not large. [CODE]a = 1 if a > … | |
Re: A quick example of a spiral using deque. It still has a few problems (works best with a square and prints too many) but shows the general concept. [CODE]from collections import deque ##======================================================================== def print_spiral( total_cols, total_rows ) : d_list = [] ## hold pointers to separate deque objects for … | |
Re: The python.org website has a beginners guide that includes links to tutorials and introductory books. [url]http://wiki.python.org/moin/BeginnersGuide[/url] | |
Re: I don't have much experience with this, so only have used ps to a file and parse the file when this is necessary. This may require some tweaking.[CODE]def ps_test(): fname = "./example_ps.txt" os.system("ps -AFL > " + fname) ## or subprocess data_recs = open(fname, "r").readlines() for rec in data_recs: if … | |
Re: [QUOTE]So i searched the sys.path for 2.6 and included usr/lib/python2.6/dist-packages in python3 sys.path[/QUOTE]So you are obviously using PyQT4 built for Python 2.6. The error message confirms it. [QUOTE]undefined symbol: PyString_FromString[/QUOTE]Ask how to install PyQT for both 2.6 and 3.x on the Ubuntu or PyQt forum. Some one has done this … |
The End.