169 Posted Topics
Re: Actually, it seems fine. I have no problems with magic(5) and magic(7). Only trouble I had was your %2d format was too narrow for magic(15), but a quick fix to that worked as well. Why do you think it's not working? | |
Re: Don't you want to say something like [ICODE]a,b = lasin()[/ICODE] instead of just [ICODE]lasin()[/ICODE]? You understand, of course, that the variables [B]a[/B] and [B]b[/B] inside function [B]lasin()[/B] are local to that function call, and disappear completely and forever after the [B]return[/B]. They bear no relation whatsoever to the [B]a[/B] and … | |
Re: Suggestion: print out [B]mrate[/B] after executing the statement [ICODE]mrate = 1/1200[/ICODE]. Then try [ICODE]mrate = 1./1200.[/ICODE] instead. Also, please use the [noparse][code=Python] and [/code][/noparse] tags around your code so it comes out properly formatted. ![]() | |
Re: [ICODE]c= raw_input (" please enter an integer ")[/ICODE]Say you enter the number 4. What is [B]c[/B] after that? It is the [B]string[/B] "4", and the [B]range()[/B] function does not accept strings in [ICODE]for j in range (1,c):[/ICODE]. Quickest way out: [ICODE]for j in range (1,int(c)):[/ICODE]which will convert the string "4" … | |
Re: I suggest starting with [URL="http://docs.python.org/lib/module-os.path.html"]os.path[/URL]. Import the [B]os[/B] module and use [ICODE]os.path.walk()[/ICODE] and [ICODE]os.path.join()[/ICODE] to get the filename syntax right on all OS versions. | |
Re: I suggest representing every bigram as a tuple (first,second), so you would parse "Every man has a price. Every woman has a price." into lower case, drop punctuation, forget the border cases and you end up with: [code]("every","man") ("man","has") ("has","a") ("a","price") ("price","every") ("every","woman") ("woman","has") ("has","a") ("a","price")[/code] Then use each bigram … | |
Re: Most Python add-on modules need to be reinstalled, usually from a new download specific to the version of Python. You need to pick up a new [B]pywin[/B]. Is there any reason you didn't upgrade to Python 2.5? It has a number of nice features you may want to use some … | |
Re: [QUOTE]But now I'd like to only make the change if there is an exact match. So the search string "ippy" should no longer cause "slippy" to change.[/QUOTE] Don't you just want to test for equality then? Say (e.g.):[ICODE]if line[:-1] == 'ippy':[/ICODE] instead of using string.find()? That would only match "ippy". … | |
Re: What kind of program do you have so far? (See [URL="http://www.daniweb.com/forums/announcement114-2.html"]the homework policy[/URL]). | |
Re: Start with this, off the cuff:[code=Python] file = open('blah.txt','r')#specify file to open for line in file: # Do the following for every line in the file, one at a time print line[0:15] # Slice off the first 15 characters file.close()[/code] Doesn't [I]quite [/I]do what you want, but close enough to … | |
Re: You almost have it. Change [code=Python]while sum <=x: b= random.randrange (1,x-1)[/code] to [code=Python]while sum <x: # Not <= b= random.randrange (1,x-sum+1)[/code] | |
Re: To the extent I understand your question, it seems to me that instead of [ICODE]obj.__name__[/ICODE] you should grab [ICODE]repr(obj)[/ICODE]. The default [ICODE]__repr__()[/ICODE] method would return something like [ICODE]'<bound method MyClass.multiply of <__main__.MyClass instance at 0x009E8968>>'[/ICODE] which is not quite what you want -- but note it is a string you … | |
Re: You didn't say whether you meant 5-card stud or 7-card stud. I assume 7-card since only geezers like me play 5-card stud. My guess is hold'em would be easier. (1) The number of down cards is smaller, (2) there's one fewer betting round and (3) the future outcomes are much … | |
Re: How about [ICODE]count[(verb,prep)] += 1[/ICODE]? | |
Re: Change [ICODE]N = sys.argv[1][/ICODE] to [ICODE]N = int(sys.argv[1])[/ICODE]. Otherwise you're comparing a number to a string, since that's what sys.argv[-] returns. Of course, then you've probably got to add an exception handler for when sys.argv[1] isn't convertible to a number, but that's for later... | |
Re: You need [ICODE]exec compile(str, '<string>', 'exec')[/ICODE] Alternatively, if str is just an expression (like[ICODE]add(2,3)[/ICODE]) you can do an [ICODE]eval(str)[/ICODE] and get back 5. So [B]exec[/B] for statements, [B]eval[/B] for expressions. | |
Re: I don't really understand your description of the problem, but the lambda form looks like it might be wrong. Where you have [ICODE]lambda: call(li_all[i])[/ICODE] are you sure you don't mean [ICODE]lambda [B]i[/B]: call(li_all[i])[/ICODE]? | |
Re: Here's an example to get you started.[code=Python]import MySQLdb conn = MySQLdb.connect (host = "nephews", user = "huey", passwd = "dewey", db = "louie") cursor = conn.cursor () table_name = "kumquat" nlines = cursor.execute ("DROP TABLE IF EXISTS " + table_name) for x in range(nlines): print cursor.fetchone () cursor.close () tdesc … | |
Re: Maybe I'm off the mark here, see if this helps:[code=Python]>>> test = TData('pork','beans') >>> v = dir(test) # --> ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'oc', 'via'] >>> eval( "test.%s" % v[-1]) # --> 'beans'[/code] For a class instance [B]X[/B], dir([B]X[/B]) … | |
Re: Lisa says: Please put code samples in between [noparse][code=Python][/noparse] and [noparse][/code][/noparse] brackets so it displays properly. As near as I can tell, moveHomer() returns a pair (row,column) and you are trying to unpack that into a 4-tuple. That just won't work. | |
Re: I've done this several times before, but not in Python. A simple approach is to give each hand a numerical ranking, and there are a couple of ways to do that. One way is to assign each hand a number, specifically the number of hands it beats. Thus 7-5-4-3-2 offsuit … | |
Re: One problem is the return statement at the start of ask_hours(). [code=Python] def ask_hours(): return input ("How many hours did you work? ") # Unconditional return from ask_hours() rate = raw_input ("What is your rate of pay? ") # <-- this statement is not part of ask_hours()[/code]You could make up … | |
Re: Like this? print "%-20s %4d" % ("John Smith",107) print "%-20s %4d" % ("Phineas T. Fogg", 80) John Smith 107 Phineas T. Fogg 80 Familiarize yourself with the % operator, with operands of the form `<string> % <sequence>` (as opposed to numeric operands, where % is quite different). | |
Re: Try [ICODE]print time.strptime(timestamp,"%Y%m%d")[/ICODE]since the year comes first in your example. | |
Re: I use pyplotlib and found it pretty easy to copy the example code. Actually I did have to do a lot of repititions of trying different things until I got the desired results, but overall it was OK, definitely worth the effort. Yes it requires numpy but you can find … | |
Re: Well, that depends upon what you mean ... :) Can you keep track of those queries where the user gets back 0 results? Just save the request to a file. Then periodically review the file yourself and manually add the definitions. Python dictionaries could be used there. So you might … | |
Re: Look [URL="http://www.thescripts.com/forum/thread432006.html"][B]here[/B][/URL] for some discussion and examples on the topic. You would have to try each node one at a time, but that's what computers are for... Of course your computer may have multiple NICs and therefore multiple subnets to talk to. | |
Re: I probably don't understand your question, but you should be OK as long as you actually "execute" the [icode]def[/icode]statement. Just having a function definition in your file is not enough, you've got to execute it. Thus [code=Python]def foo(bar): print "BAR", bar foo("Hello!")[/code]works, whereas[code=Python]foo("Hello!") def foo(bar): print "BAR", bar[/code]fails. | |
| |
Re: Normally I would accomplish this with a separate "timer thread" that looks for time to pass and calls the requested function when appropriate. In that case I'd have the timer thread sleep for a second, call the function, lather, rinse, repeat... Why do you not want to use time.sleep()? Do … | |
Re: Presuming you have all this in separate variables as indicated, try something along these lines: [code=Python]sh, sm, st = 12, 30, 'pm' eh, em, et = 6, 0, 'pm' # First, adjust "hour reported" to real "hour of day" if sh < 12 and st.lower() == 'pm': sh += 12 … | |
Re: The only even number you need try is 2. So I suggest you replace [ICODE]for x in range(2, int(n**0.5)+1):[/ICODE] with [ICODE]for x in [2]+range(3, int(n**0.5),2):[/ICODE] Also remember a number may have multiple equal prime factors, e.g., 40 = 2*2*2*5. | |
Re: If you [I]really[/I] need to count and sum the perfect squares up to 10^20, you could just as easily take all the numbers up to 10^10 and square them, thereby cutting your runtime by an order of magnitude or two... You could observe that there are exactly 10^10 entries in … | |
Re: If you mean an array in the numpy sense. e.g., [ICODE]z = numpy.zeros([5,3],float)[/ICODE] then the [B]shape[/B] attribute is what you want, e.g. [ICODE]z.shape[/ICODE] returns (5,3). If you're implementing an array as a Python list, then [ICODE]len(z)[/ICODE] is what you want. | |
Re: [ICODE]s = "You're sure that would work on this string?"[/ICODE] | |
Re: [ICODE]put <filename> flash[/ICODE] is an FTP client command that says "Take my local <filename> and copy it to the server as a file named flash". So the correct call would be [ICODE]ftp.sendcmd('STOR flash')[/ICODE] It would then be up to you to follow that up with data from the local file … | |
Re: One possibility, depending on how you run your tasks, is to pass the IP address on the command line, thusly: [ICODE]C:\Work>blort.py 10.3.21.109[/ICODE]. The IP address would show up in the program as [ICODE]sys.argv[1][/ICODE] in this case:[code=Python]C:\Work>type blort.py import sys print sys.argv C:\Work>blort.py 10.3.21.109 ['C:\\Work\\blort.py', '10.3.21.109'] [/code] The IP address is … | |
Re: I suppose the direct attack would look like: [code=Python]def common_func(arg, sub_func): statement1 statement2 statement3 if sub_func==1: code for option 1 elif sub_func==2: code for option 2 etc. statement4 statement5[/code] Then if you like: [code=Python]my_first_func = lambda x: common_func(x,1) my_other_func = lambda x: common_func(x,2)[/code] (or something like that, I forget the … | |
Re: There's nothing wrong with asking for help and nothing wrong with being a novice. But when you ask for help, around here at least, it is expected you will wrap your code in code-tags as already explained. If nothing else, it increases the odds someone will look at it. When … | |
Re: [QUOTE=Delta424;521940][...]I wanna make a program that can generate random numbers, letters, or words (in 3 different programs of course). If anyone could help, it would be great! :)[/QUOTE] Probably the first place to look is the [B]random[/B] library. Specifically these functions: [code=Python] >>> import random >>> print random.random() 0.37449984017 >>> … | |
Re: [QUOTE=nirmalarasu;532271]...Currently i am writing telnet python script for connecting remote server. I would like to save the telnet session activites in a file. Is there any better way to grap the remote output in a local machine file.[/QUOTE] I don't know what you mean by "better". I have managed to … | |
Re: [QUOTE=KomodoM;532164]Hi! I'm trying to program a monthly calendar.... but I can't get the weekdays for the nth day of the year right... [/QUOTE] Have you looked far enough into the [B]time[/B] module? The tm_wday element of the struct_time is exactly what you want. Or is there some reason you can't … | |
Re: I looked into these things a while ago and was a bit surprised to read that the swiper threads its input data into the keyboard, via a driver it comes with. In that case you can use standard keyboard reads such as raw_input(). Alternatively some swipers come with cables to … | |
Re: 457 Elegantly, the sum of three consecutive primes. 149 + 151 + 157 = 457 | |
Re: [QUOTE=Duoas;508278]...There is no way to prevent someone from copying your program to another PC. To date, the best way to prevent someone from executing your program on an unauthorized PC is by using a dongle, which is not cheap to manufacture or buy. ...[/QUOTE] Agreed, a dongle is a good … | |
Re: When providing sample code, please use [noparse][code=python][/noparse] (or is it [noparse][code=Python][/noparse]?) and [noparse][/code][/noparse] tags so as to produce accurate spacing and syntax highlighting. That said, I don't know the answer off the top of my head. But [LIST=1] [*]What is that naked def doing at the front of the code? … | |
Re: If you mean [B]decimal[/B], not [B]float[/B], I don't see the need for anything special. E.g., [code=Python] >>> import decimal >>> x1 = decimal.Decimal("4.567567567") >>> x2 = decimal.Decimal("4.56756757") >>> if x1==x2: ... print "OK!" ... >>> if x1<x2: ... print "OK!" ... OK! >>> [/code] | |
Re: You don't need split(). The reader() method returns a list already split. If you look at what [inlinecode]print line[/inlinecode] outputs you should see: ['04/04/2006', '1305.93000000']. So your code should read something like: [inlinecode] TradingDate, MarketIndex = line[0], line[1][/inlinecode] Of course you'll need to convert the data from [B]string[/B] datatype to … | |
Re: [inlinecode]cp.read ("feedmonitor.cfg")[/inlinecode] When running as a Windows service you probably have a different default directory. It may not be finding the configuration file you expect. | |
Re: You probably needed to say [inlinecode]copy1 = temp()[/inlinecode], otherwise copy1 is a copy of the class temp instead of an instance of the class temp. |
The End.