2,190 Posted Topics
Re: Add some print statements to see what is going on. I have added two in the following code.[CODE]from random import choice as rc def total(hand): aces = hand.count(11) t = sum(hand) if t > 21 and aces > 0: while aces > 0 and t > 21: t -= 10 … | |
Re: I like using sets because they have a difference function.[CODE]alphabet = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ") keyword = raw_input("Enter word ").upper() difference = alphabet.difference(keyword) ## sets are in hash order not in alphabetical order print "Hash sequence", difference, "\n" ## convert to a list so it can be sorted difference = list(difference) difference.sort() ## … | |
Re: [QUOTE]Once the user gives the program these parameters I would like to put it into the file below on line 11 and re save it as Compare # 2.asc.[/QUOTE]Apparently he does want to change the file posted, but since every tutorial covers file read/write, and no effort has been made … | |
Re: l = abs(math.sqrt(2)(r*r)-(y*y)) Break this formula down into one calc per line and see where the error occurs, then print the variable and it's type(variable_name), and that should tell you where you went wrong. Also, it is not polite to use "i", "l",. or "o" as single letter variable names. … | |
Re: I've modified you code by adding a function to do the "HTH" and "HTT" search. Since it is basically the same for 'H' and "T', one function can do it instead of two blocks of the same code . Note that the if heads/tails > 0 appears to be worthless, … | |
Re: I just skimmed your code, but it looks like a set() will do some of this for you. Using a builtin is easier and more reliable as it has been tested thousands of times. And cudos for figuring out the logic.[CODE]user_input = ["three", "four", "five", "six", "seven", "eight", "nine", "ten","one", … | |
Re: [QUOTE]# changes the system's current dir. path to specified path[/QUOTE]It doesn't change the path, it adds that path to the list that Python searches. You can print sys.path if you want to see what it is. sys.path.append("/engine/lib/") ## or whatever it is import sprite_class and then sprite_obj = sprite_class.Sprite() to … | |
Re: There are a few links to tutorials here. Qt seems to be the least documented of all the GUI toolkits for Python. [URL]http://www.python-eggs.org/?row=2[/URL] | |
Re: You will have to link them some way. You can combine them and sort as below, otherwise you will have to associate "yes" with zero, "i don't know" with one, etc. and reorder the list named results after sorting the labels list, but there isn't any reason why you have … | |
Re: You can replace this:[CODE]#precondition to the actions checker def precondition(rule,state): if not blocked(rule,state,(allDirections[0])): if not blocked(rule,state,(allDirections[1])): if not blocked(rule,state,(allDirections[2])): if not blocked(rule,state,(allDirections[3])): if not blocked(rule,state,(allDirections[4])): if not blocked(rule,state,(allDirections[5])): if not blocked(rule,state,(allDirections[6])): if not blocked(rule,state,(allDirections[7])): return True else: return False ##--------------------------------------------------------- ## with (not tested) ## in other words, if any … | |
Re: You should not be using "is" for comparison. It is used to test if the objects are the same. Guido, et al decided to store an internal list of 1-256 and reference the list for those numbers, so if you try: x=257 print x is 257 it will print False … | |
Re: [QUOTE]Of course, I could do simple list comparison.[/QUOTE]However you organize it, you will have to compare all fields, i.e. cow_list[1] == sheep_list[1] = number of legs ==> ctr += 1 A hashed db or MySQL would only improve things if you were looking for all animals with 4 legs as … | |
Re: You can also use divmod.[CODE]cnum = 21 cntnu = 1 currint = 1 #Begin Loop while cntnu == 1: currint = currint + 1 whole, remain = divmod(cnum, currint) if 0 == remain: cntnu = 0 print "Factorization Found." print whole, remain print currint, cnum else: print currint, "Factorization failed. … | |
Re: You don't have to compile Python programs. It is interpreted/byte compiled, which basically means that it takes care of that. If you are just starting, I would suggest that you use Idle (comes with Python) until you are more comfortable with the language. A somewhat dated link [url]http://www.ai.uga.edu/mc/idle/index.html[/url] | |
Re: If you just want to convert text, there are tools like txt2pdf. You'll have to Google "text pdf" to see what runs on your OS. | |
Re: [QUOTE]take printout for the output of that program[/QUOTE]Do you mean save it in a file, send to the printer???[QUOTE]don't judge me because I'm a year 8![/QUOTE]My new sig: "Don't judge me because I'm a dumb-ass". | |
Re: [QUOTE]Can the edit wizard be altered to allow more than 4 packs to be used?[/QUOTE]And you will have to post your "edit wizard" code in order to get advice on how to change the program. | |
Re: [QUOTE]I need the window to just refresh each time.[/QUOTE]I think you want root.update(). This is a simple example that I had lying around (click the start button to test).[CODE]from Tkinter import * import time ## --------------------------------------------------------- ## Demonstration of window update from FAQTs ## --------------------------------------------------------- class TestUpdate: def __init__(self, top): … | |
Re: Python (with batteries included) usually has a tested tool that will already do most common things. In this case, you want sets.[CODE]list1 = ['spam', 'eggs', 'bacon'] list2 = ['spam', 'ham', 'king richard'] set_1 = set(list1) set_2 = set(list2) ##matchitems(list1, list2) new_set=set_1.intersection(set_2) print new_set ## works either way print set_2.intersection(set_1) # … | |
Re: Do you just want to extract the values for the "MATERIAL" and "STEEL" or do you want all values under "MATERIAL"? Ditto for "FRAME" and "SHELL". You would test for "MATERIAL" or "FRAME" and then extract depending or which catagory it is, and use a dictionary to store the values, … | |
Re: To start with, what do these mean and what is the order of their evaluation? Post your answer here if you want some help.[code]if (y>x and y<z)or(y<x and y>z): elif not(x!=y and y!=z):[/code]Since you have code, you can answer the first question by brute force. Set x and y to … | |
![]() | Re: Something like this, using an interim list (untested). You could also put this in a function and return the word and counter the first time it is found.[CODE]logfile = open("logfile.txt", "r").readlines() KEYWORDS = ['test', 'text'] counterline = [] counter = 0 for line in logfile: junk_list = [] for word … |
Re: I think the problem is this line, if I understand you correctly. self.entryb["variable"] = self.bringout I think you can only use "StringVar()" and "textvariable=" with a button since there is no data entry or changing of the text, so change the code to something like[CODE] self.bringout = StringVar() self.bringout.set("0" ) … | |
Re: You might start with code tags (use the "#" icon) and then explain what the program you posted does, as it doesn't appear to make sense. Once you reach this line while R[0]<R[1]: it appears to be an infinite loop, but it's difficult to tell without proper indents. Basically, you … | |
Re: For your example, the position is found, i.e. True is returned. Consider using "in" when comparing the two lists as that is the preferred method. The following illustrates this.[code]a = [ [0,1], [0,2], [1,2] ] b = [0, 2,] c = [2, 0] if b in a: print "b found" … | |
Re: Please put your code between code tags (the "#" icon) as no one will be able to run your program without proper indents. Generally, you use a control variable, in your case a string control variable, and the ".set " and ".get" methods to change the text or get the … | |
Re: Test each function individually by inserting print statements. Comment everything you don't want by using triple quotes """. Since you only have one while() loop, start here.[code]while not goal(state): print "=====>while not goal(state) ", goal(state), state for l in range(0, (len(allRules)), 1): if precondition((allRules[l]),state): applyRule((allRules[l]),state) else: print "Rule cannot be … | |
Re: A nice explaination of subprocess, which every programmer will use at one time or another, for future reference. [url]http://blog.doughellmann.com/2007/07/pymotw-subprocess.html[/url] | |
Re: When someone enters pay rate or hours, what happens if they accidentally enter a letter or a float instead (pay rate is $9.99)? Or does the problem let you assume that only integers are entered,[code]pRate=raw_input("What is their hourly rate? ") intRate=int(pRate)[/code] | |
![]() | Re: [quote]I can loop through all three questions ONE time and that's it.[/quote][CODE]while hours<1 or hours>60: hours=int(raw_input("Enter the number of hours[/CODE]If you entered 42 hours on the first pass, then hours still equals 42 and you will not enter the while loop. See Shibby's snippet. Use one infinite while() loop, to … ![]() |
Re: I use pyprocessing for this. For Python 2.5 it is pyprocessing, for 2.6 and above it is included in the distro and named multiprocessing. [url]http://pyprocessing.berlios.de/[/url] This will exit when you hit <Enter>.[CODE]import subprocess from processing import Process class test(): def funct_1(self, name): subprocess.Popen("ping www.google.com", shell=True) if __name__ == '__main__': CT=test() … | |
Re: It seems that all you have done is to copy code you found somewhere else. For example, what does this line that you posted mean and why would it be used in a fib sequence? a, b = b, a + b Start with a routine to get the number(s) … | |
Re: Not tested but should be close. In these types of programs, you want to set a variable to assume that there are errors and unset it only when all conditions are met.[code]errors = True while errors: age = input('How old are you today? ') try: age_int = int(age) if (age_int … | |
Re: I have a symlink from /usr/bin/python to python2.5 as the default. If I want to use 2.6 or 3.x I simply use the shebang #!/usr/bin/python3.0 They install into different library directories, so multiple versions isn't problem. | |
| |
Re: This "is" operator is meant to be used to test for the same object, not equality. Try this on your computer a=257 print a is 257 <prints "False"> So it can give you unexpected results. You want to use: [code] a = True while a : a = False ## … | |
Re: This line doesn't make any sense.[code]x = "a" or "b" or "c" or "d" or "e" or "f" or "g" or "h" or "i" or "j" or "k" or "l" or "m" or "o" or "n" or "q" or "r" or "s" or "t" or "u" or "v" or "w" … | |
Re: You would use a dictionary to convert.[code]convert_dic = {"I":1, "V":5, "X":10, "L": 50, "C ":100, "D": 500, "M": 1000 }[/code]Also, this line should be "equal to or greater than". if romaninput[0] > romaninput[1]: Better yet, use a for loop to iterate through all of the roman numerals. Sorry if this … | |
Re: Depending on the version of Python you are using, this may or may not return a string hours = input("How many hours did they work?: ") If it is a string and not a float print type(hours), the function should return float(hours) Hopefully, it is obvious why you want to … | |
Re: Python also has smtplib if you are referring to e-mail messages. | |
Re: You are over-thinking this[CODE]list_n = [[3, 2], [5, 1], [4, 7]] print "Before", list_n for j in range(0, len(list_n)): row = list_n[j] row.append(0) ## can also use just list_n[j].append(0) print "After ", list_n [/CODE] | |
Re: Start with just this part and post the code here.[QUOTE]Using a loop, allow the user to input as many student names as they like[/QUOTE] | |
Re: You can specificy a different delimiter. The code below is from the official docs. But for an either/or situation, if you don't want to write your own routine, the easiest is probably reading each file, replacing a semicolon with a comma if found, and writing to a new file. Then … | |
Re: Check Pmw and Tix. Here is an example with Pmw [url]http://www.java2s.com/Code/Python/GUI-Pmw/GaugemadefromPmwMegaWidget.htm[/url] | |
Re: I think you just want a simple menu program[CODE]def func_a(): print " hello world import" def func_b(): print " hfile import" ##------------------------------------------------------------------- menu_d = { "awesome1":'func_a()', "/help":'func_b()'} choice = "" while choice not in menu_d: choice = raw_input("type /help to continue or the password:\n ") if choice in menu_d: eval(menu_d[choice]) … | |
Re: This online book has a good explanation of arguments [url]http://en.wikibooks.org/wiki/Python_Programming/Functions[/url] After that, the best way to learn is to try a few things yourself, print the results, and see what is happening. | |
Re: Also note that in the following code you convert to upper case after testing for an upper case letter. You want to convert to upper() once only directly after the input. [CODE] if userit == A: userit = userit.upper() print ("\n Thats good. I like it too ") ,userit ,[/CODE]This … | |
Re: Also, add the code for how you call these functions if you post again. Some errors are simply caused by a function not being called with parameters, etc. and since you don't print or return from "sides" there is a potential problem there as well. Edit: I see that you … | |
Re: The problem may be that you are using the reserved word "file" and perhaps overloading it. file and open are basically the same for Python. So use something other than "file", check the contents of the output file with a text/word processor to see that it is really there 3 … | |
Re: There are two separate parts of course. Start with the logic 1. How are you going to identify the individual squares, by row and column, or will each square have a unique number. And how will you test to see that the square is not already occupied, which will be … |
The End.