User Profile
Collapse
-
Try using a set, they're unordered an I believe faster than a list it also supports unions, intersections and differences which may or may not help
Code:>>> s = set(i for i in xrange(101)) >>> s set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
Leave a comment:
-
You could overload the __repr__ function to fix that.
Code:>>> class C: def __repr__(self): return "Test" >>> C <class __main__.C at 0x860e68c> >>> C() Test >>> [C(), C(), C()] [Test, Test, Test]
Leave a comment:
-
You're only storing the last created person. You could use a list to store all of the people created.
[code=Python]run = 1
class Person:
people = []
def __init__(self, name):
self.name = name
Person.people.a ppend(self)
def sayHi(self):
print 'Hello, my name is', self.name
def countPeople(sel f):
print 'There are %d people...Leave a comment:
-
You can use django, http://www.djangoproje ct.com/, as the framework. Here's a tutorial on howto create a wiki with it,http://showmedo.com/videos/series?name=v7k ABKL6R....Leave a comment:
-
Try replacing it with a while loop. A while loop will continuously execute all the code inside while the condition is true. Also the keyword break will exit the loop.
[CODE=python]
while someCondition:
# execute some code
[/CODE]...Leave a comment:
-
Just start the block with [code=python][code=python][/code]
But I think he means something like
[CODE=python]
class Foo():
def __init__(self):
self.b = Bar()
class Bar():
def __init__(self):
self.f = Foo()
f = Foo()
[/CODE]
A fix would be something like
[CODE=python]
class Foo():
def __init__(self, bar=None):
self.b...Leave a comment:
-
I believe their is a module to read and write excel files but you could always just save it as a CSV file (comma separated values) and write a quick reader for it.Leave a comment:
-
You can access nested lists the same you could try something like li[i][j] but the following is more scalable
[code=python]
if True:
lol = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print "Columns %s"%( " ".join([str(j) for j in range(len(lol[0])]) )
for i in range(0,len(lol )):
print " Row %s: %s"%(i, " ".join( [str(j) for j in lol[i]]))
[/code]...Leave a comment:
-
Use try except to handle errors.
[code=python]
try:
n = int( raw_input("Ente r a number: ") )
print "n=%s, n squared=%s"%(n, n*n)
except ValueError:
print "Invalid input."
[/code]Leave a comment:
-
Can you post some code. Use a for loop for the loop.
[code=python]
def flipCoin():
#...
for i in xrange(100):
print flipCoin()
[/code]...Leave a comment:
-
-
Django seems to accomplish this although it may do it this way.
[code=python]
import sys
def sayHello(name):
print "Hello, %s!"%name
def main():
if len(sys.argv) >= 2:
if sys.argv[1] == "sayHello" and len(sys.argv) == 3:
sayHello(sys.ar gv[2])
main()
# output in command prompt
python...Leave a comment:
-
Django seems to accomplish this although it may do it this way.
[code=python]
import sys
def sayHello(name):
print "Hello, %s!"%name
def main():
if len(sys.argv) >= 2:
if sys.argv[1] == "sayHello" and len(sys.argv) == 3:
sayHello(sys.ar gv[2])
main()
# output in command prompt
python...Leave a comment:
-
Since the format for a script is "python test.py function arg1 arg2 .. .argx"(without quotes) an exe would probably be somthing like "test[.exe] function arg1 arg2 .. .argx"(also without quotes)...Leave a comment:
-
Functions need to be called on to run. also no code can go after the return statement.
[code=python]
def square(x):
n = x*x
print "hello world"
return n
square(2)
[/code]
Or try removing the quotes and something like this, python test.py square(2), should work...Leave a comment:
-
It looks like your missing the extension.
ie. *.jpg *.gif *.png etc.Leave a comment:
-
You can escape quotes with "\" but it usually better to avoid it unless you're nesting multiple layers of quotes.
[code=python]
>>> print "\""
"
>>> answer = 15
>>> print "\"%s\""%(answe r)
"15"
[/code]Leave a comment:
No activity results to display
Show More
Leave a comment: