880 Posted Topics

Member Avatar for walker164

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) …

Member Avatar for walker164
-1
150
Member Avatar for SomewhereInKS

[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]

Member Avatar for snippsat
0
238
Member Avatar for squareff255

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. …

Member Avatar for snippsat
0
92
Member Avatar for chico2009

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 …

Member Avatar for chico2009
0
186
Member Avatar for etypaldo

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 …

Member Avatar for sneekula
0
165
Member Avatar for mahela007

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): …

Member Avatar for djidjadji
0
175
Member Avatar for AutoPython

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. …

Member Avatar for AutoPython
0
331
Member Avatar for chico2009

[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 …

Member Avatar for chico2009
0
194
Member Avatar for AutoPython

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 >>> …

Member Avatar for AutoPython
0
192
Member Avatar for someone112
Member Avatar for hydrobat

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.

Member Avatar for hydrobat
0
1K
Member Avatar for leegeorg07

if word not in never_save.readlines(): You need [ICODE]:[/ICODE] or SyntaxError.

Member Avatar for woooee
0
118
Member Avatar for Lingson

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 …

Member Avatar for Lingson
0
262
Member Avatar for Dan08

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]

Member Avatar for Dan08
0
155
Member Avatar for Morika

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'] >>> …

Member Avatar for Morika
0
171
Member Avatar for Missy_poo

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 …

Member Avatar for Stefano Mtangoo
0
104
Member Avatar for fallopiano

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 …

Member Avatar for snippsat
0
270
Member Avatar for b_bayaraa_d

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]

Member Avatar for snippsat
0
55
Member Avatar for sravan953

[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): ''' * …

Member Avatar for Stefano Mtangoo
0
225
Member Avatar for StMark

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__', …

Member Avatar for StMark
0
214
Member Avatar for Clueless86

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 …

Member Avatar for jlm699
0
414
Member Avatar for sravan953

[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 …

Member Avatar for bumsfeld
0
132
Member Avatar for Aiban

[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 …

Member Avatar for snippsat
0
108
Member Avatar for abhigyan91

[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) …

Member Avatar for shadwickman
0
143
Member Avatar for tomtetlaw

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]

Member Avatar for scru
0
110
Member Avatar for Your_mum

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]

Member Avatar for Your_mum
0
137
Member Avatar for fallopiano

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 …

Member Avatar for fallopiano
0
116
Member Avatar for flipjoebanana
Member Avatar for Stefano Mtangoo
0
153
Member Avatar for zachabesh

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 …

Member Avatar for zachabesh
0
92
Member Avatar for lblazer05

Se if this help. [code=python] >>> a = '1e+12' >>> a '1e+12' >>> b = a.replace('e+', '*(10**') + ')' >>> b '1*(10**12)' [/code]

Member Avatar for snippsat
0
133

The End.