[Tutor] Compound if statement question.

2011-05-01 Thread Greg Christian
Is there a way to write an if statement that will pick up duplicates (two ‘1’s):

L = ['1', '4', '1']
if (L[0]) != (L[1]) != (L[2]):
print "THEY ARE NOT EQUAL"
else:
print "THEY ARE EQUAL"

When I run this code, it prints “THEY ARE NOT EQUAL” when it should print the 
else “THEY ARE EQUAL”.

list L has two ‘1’s; therefore I am trying to get an if statement that will 
recognize this. When using the != (not equal) operator, shouldn’t the if be 
true when items in list are not the same? Any input would be appreciated.

Thanks,

Greg___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Reading elements in a file

2011-05-04 Thread Greg Christian
I am kind of stuck on what is probably a simple thing:

If we have a file of words like this: 
“first”,”word”,”in”,”that”,”another”,”part”,”of”,”this”

f = open('words.txt', "r")
words = f.read()

will read the whole file, is there a way to read just the words: first word in 
that another part of this

I guess I have to separate on the “,” but I am not quite sure how to go about 
this.

Any input would be appreciated.

Regards,

Greg___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Reading elements in a file

2011-05-04 Thread Greg Christian
Python version = 2.7.1
Platform = win32

I am kind of stuck on what is probably a simple thing:

If we have a file of words like this: 
“first”,”word”,”in”,”that”,”another”,”part”,”of”,”this”

f = open('words.txt', "r")
words = f.read()

will read the whole file, is there a way to read just the words: first word in 
that another part of this

I guess I have to separate on the “,” but I am not quite sure how to go about 
this.

Any input would be appreciated.

Regards,

Greg___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How is the return statement working in this function?

2012-04-05 Thread Greg Christian
I am just wondering if anyone can explain how the return statement in this 
function is working (the code is from activestate.com)? Where does x come from 
– it is not initialized anywhere else and then just appears in the return 
statement. Any help would be appreciated.


def primes(n):
"""Prime number generator up to n - (generates a list)"""
## {{{ http://code.activestate.com/recipes/366178/ (r5)
if n == 2: return [2]
elif n < 2: return []
s = range(3, n + 1, 2)
mroot = n ** 0.5
half = (n + 1)/2 - 1
i = 0
m = 3
while m <= mroot:
if s[i]:
j = (m * m - 3)/2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i + 1
m = 2 * i + 3
return [2]+[x for x in s if x]___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] TypeError: 'int' object is not callable

2012-05-16 Thread Greg Christian
Can anyone tell me what I am doing wrong here. When trying to call the factors 
function from main with x = factors(Tn), getting the error message: “TypeError: 
'int' object is not callable”? Any help would be appreciated. Thanks.


def factors(n):
L = []
for i in range(1, int(n ** 0.5) + 1):
if (n % i == 0):
L.append(i)
return L

def main():
factors = 0
counter = 0
L = []
while len(L) < 50:
counter += 1
L.append(counter)
Tn = sum(L)
x = factors(Tn)
#print x
print(sum(L))


if __name__ == '__main__':
main()___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using sorted in Python 3.3.5

2015-11-19 Thread Greg Christian
I’m trying to sort a list of tuples based on the second item in the tuple. When 
I run this in IDLE I get the correct output; however, when running inside of a 
program, and calling the counter() function, sorted does not seem to work? Any 
ideas on why this works in IDLE and not in program would be appreciated. Thank 
You.

def getKey(item):
return item[1]

def counter():
L = [("baby", ), ("aeuron", 100), ("pablo", 1234)]
sorted(L, key=getKey)
print ("L = ", L)

OUTPUTS THIS (when calling counter inside of program):

L =  [('baby', ), ('aeuron', 100), ('pablo', 1234)]

OUTPUTS THIS (when running with IDLE – desired output):

[('aeuron', 100), ('pablo', 1234), ('baby', )]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor