880 Posted Topics
![]() | Re: indentations is 4 spaces,it work with 2 and 8 but never ever us anything else than 4. A simple fix move line 8 so it get input from loop. Remove set [CODE]import sys result = [] f = open("sample_text.txt") for line in f: for word in line.split(): if word.startswith('h'): result.append(word) … ![]() |
Re: [CODE]>>> x = 813367.216157178627327 >>> x 813367.21615717863 >>> b = '%.2f' % x >>> b '813367.22' >>> f = open('poly.txt', 'w') >>> f.write(b) >>> f.close() >>> a = open('poly.txt', 'r') >>> a.read() '813367.22' >>> [/CODE] | |
Re: Nice try. But you have done some mistake. [code]lower = trick/2+low #originally 250... higher = trick+lower #originally 750...[/code] This is upp to the user,by typing 500,250.. to guess the number. The user is doing a [url=http://en.wikipedia.org/wiki/Binary_search_algorithm#binary%20search]binary search[/url] It was easier for me to write this than start edit your code. … | |
Re: Just a littel change from the good suggestion from woooee. Type a letter:@ Never trust the user,there are always someone that dont follow instruction well. If you want to look into this it`s called [URL="http://docs.python.org/tutorial/errors.html"]Exceptions handling[/URL] The other choice is to write the menu so that you dont need exceptions … | |
Re: One way is like this Take numbers and add like this "A" count as 1. [CODE=python]>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + 'Jack Queen King'.split() >>> numbers [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King'] >>> >>> suits … | |
Re: Just to have some fun i build on sneekula Animal class. Here the animal has name and it can eat and poop. [CODE]class Animal(object): def __init__(self, name, legs): self.name = name self.legs = legs self.stomach = [] def __call__(self,food): self.stomach.append(food) def poop(self): if len(self.stomach) > 0: return self.stomach.pop(0) def __str__(self): … | |
Re: Yes dont use global find other soultion like OOP. Think of function like a black box you can only put in variabels and return variabels out. Then you can use your function anywhere in your code. It dont leak out in global space. Think always about design of your code. … | |
Re: [CODE=python]def Tc_to_Tf (): Tc = input ("What is the Celsius Temperature ? " ) Tf = 9.0/5.0 * Tc + 32 return Tf[/CODE] Run it an nothing happends it only return Tf Let do something simple in idle. [CODE=python]IDLE 2.6.2 >>> Tf = 50 >>> print 'The temperature is %.2f … | |
Re: To round off decimal [code=python] IDLE 2.6.2 >>> 0.0225 0.022499999999999999 >>> round(0.0225, 3) 0.023 >>> round(0.0225, 2) 0.02 >>> a = 15.1548 >>> print '%.2f' % a 15.15 >>> print '%.3f' % a 15.155 #Look at type >>> s = '1.458045577554545454' >>> type(s) <type 'str'> >>> x = 1.458045577554545454 >>> … | |
Re: Try gui2exe. [url]http://code.google.com/p/gui2exe/[/url] | |
Re: try. if shotsum > [ICODE]self[/ICODE].old_leader: Can you post [ICODE]participants.txt[/ICODE] Or just a part of it,so we can se format that goes inn. | |
![]() | Re: if word not in never_save.readlines(): You need [ICODE]:[/ICODE] or SyntaxError. |
Re: Yes most books tutorial are for 2.x No problem to have both installed 2.x and 3.x installed No problem to read 2.x books/tutorial you are learing 3.x then to,they difference is not that big. To say it simple there are to much code 2.x that will help you that just … | |
Re: If you want to open notepad. You can use the os module. [code=python] import os os.system('start notepad.exe') #Open a webpage os.system('start iexplore.exe http://www.google.com') [/code] | |
Re: You can do it like this. [code=python] >>> s = '491 43140113108107 11210' >>> s '491 43140113108107 11210' >>> def splitCount(s, count): return [''.join(x) for x in zip(*[list(s[z::count]) for z in range(count)])] >>> a = splitCount(s, 3) >>> a ['491', ' 43', '140', '113', '108', '107', ' 11', '210'] >>> … | |
Re: Not much help to give you without solving it. Look a little on this. [code=python] >>> km = raw_input('How many km? ') How many km? 100 >>> km '100' >>> #As you see km is now a string >>> type(km) <type 'str'> >>> #make intreger >>> int(km) 100 >>> #100 … | |
Re: Not quite sure what You mean,but here are som min/max method that can help [code=python] >>> a = min([0,7],[100,25],[150,59],[300,189]) >>> a [0, 7] >>> b = min(a) >>> b 0 >>> c = max([0,7],[100,25],[150,59],[300,189]) >>> c [300, 189] >>> d = max(c) >>> d 300 #same with max >>> list_of … | |
Re: Here are some good editors. [url]http://code.google.com/p/pyscripter/[/url] [url]http://www.contexteditor.org/[/url] [url]http://code.google.com/p/ulipad/[/url] | |
![]() | Re: [QUOTE]Why do I have to specify 'self' while defining the function? I usually don't do it if the function isn't in a class, then why if it is in a class[/QUOTE] In a class a function is called a method. An example. [code=python] class SimpleClass(object): def SayHello(self, string): ''' * … |
Re: Dont think you can call print,have you done that before? In Python a callable is an object which type has a __call__ method: [code=python] def test(): print 'hi' >>> dir(test) ['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', '__get__', '__getattribute__', '__globals__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', … | |
Re: Still some errors. Game are looping on one menu. Give global error on gold. If you type wrong an exception is trown(NameError) Try to avoid to use global so much. Saw you gave opp classes. That ok,but you can better with your loop-functions design than it is now. Just a … | |
![]() | Re: [code=python] def reduce(): # global p s = raw_input("Enter a number: ") x = int(s) p = 0 for i in s: p += x % 10 x = x / 10 return p, x reduce() print p [/code] Why are we getting a NameError now? Take away # and … |
Re: [QUOTE]mean also downgrading the python version just to use a different toolkit, only to be regraded later when wx works with v3 and then i reupgrade.[/QUOTE] There is now upp and downgrade in python. Most off us you 2.x,that dosent mean that we dont test python 3.x out. Have off … | |
Re: [QUOTE]That ought to work! [/QUOTE] Not work maybe typeError. 'a' iterarte throught the list,then you have to compare [ICODE]'a ' == 'pear'[/ICODE] Now you compare l with 'pear' [ICODE] >>> l=['apple','orange','mango','pear','strawberry'] >>> l == 'pear' False >>> [/ICODE] [code=python] l = ['apple','orange','mango','pear','strawberry'] for a in l: if(a == 'pear'): l.remove(a) … | |
Re: When take out data from a dictionary order is not important. But here is a soultion. [code=python] >>> a_dict = {'2':12354, '1':12355432, '4':35342, '3':858743} >>> sorted(a_dict.items(), key=lambda (v,k): (v,k)) [('1', 12355432), ('2', 12354), ('3', 858743), ('4', 35342)] >>> [/code] | |
Re: You can pass value in as argument. [code=python] new = raw_input('Enter name: ') class Test(object): def __init__(self,value): self.value = value x = Test(new) print x.value ''' output--> Enter name: hi hi ''' [/code] | |
Re: Here is a class you can look,with some basic function of a class. Paulthom12345 has a good eksample how a class can inheritance from other class. Just little fix to Paulthom12345 code. Shape.__init__(name) change to Shape.__init__([ICODE]self, name[/ICODE]) [code=python] class SimpleClass(object): '''Doc string info about class''' # Class attributes as you … | |
Re: Works fine for me to. Testet 2.5.4 and 2.6.2 wxPython 2.8. | |
Re: Here i make a copy of the list,then iterating over it. It is not a good ideĆ© to delete/add somthing from a list you are iterating over. The solution is to make a copy or new list. [code=python] mylist = ['bunnies', 'apples', 'animals', 'fruit', 'amsterdam'] for item in mylist[:]: if … | |
Re: Se if this help. [code=python] >>> a = '1e+12' >>> a '1e+12' >>> b = a.replace('e+', '*(10**') + ')' >>> b '1*(10**12)' [/code] |
The End.