Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > Other Topics > Programming

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 07-18-2012, 05:42 PM   #616
newbie
 
Join Date: Feb 2010
Posts: 35
Re: ** Python Support Thread **

Firstly, try raw_input(), secondly try forcing conversion to int, so actually use int(raw_input())
eterm is offline   Reply With Quote
Old 07-19-2012, 12:11 PM   #617
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,195
Re: ** Python Support Thread **

Quote:
Originally Posted by clowntable View Post
I like that explaination a lot, thx.


You might also find this interesting: http://www.cs.utexas.edu/users/EWD/t...xx/EWD831.html
Xhad is online now   Reply With Quote
Old 07-21-2012, 04:54 AM   #618
old hand
 
sorrow's Avatar
 
Join Date: Apr 2008
Location: Perth, Western Australia
Posts: 1,500
Re: ** Python Support Thread **

Quote:
Originally Posted by Neko View Post
sorted(_dict, lamda x: (x["points"], x["percent"])) should do it.
*mutters about gmail spam filter*

Pretty close to what I ended up with:
Code:
ladder = sorted(ladder, key=lambda d: (-d['points'], -d['percent']))
Gotta love python.
sorrow is offline   Reply With Quote
Old 07-22-2012, 12:51 AM   #619
newbie
 
WeAreSamurai's Avatar
 
Join Date: Nov 2010
Posts: 19
Re: ** Python Support Thread **

Hey guys. So sometimes I find that I want to use a function in a loop where it'll act on the arg only if it exists. Like for example do a loop on a list and keep going only until it finds out that the next index of that list doesn't exist. Or maybe the function operates on a list of various class objects, of which only some of those objects share a certain method. So if that object has that attribute, I want the function to be carried out, but if not, do nothing and move on to the next item in the list.

And the easiest way I can think of to do this is to use a try except. But I'm not sure if that's considered bad form, or if they're only supposed to be used to catch errors.
WeAreSamurai is offline   Reply With Quote
Old 07-22-2012, 01:03 AM   #620
Pooh-Bah
 
Join Date: Jul 2005
Posts: 4,001
Re: ** Python Support Thread **

A lot of people in the python use try/catch for the type of situation you're describing. Personally I'm not a huge fan of it. I'd rather do something like:

Code:
for obj in objects:
    if hasattr(obj,"method_name"):
       obj.method_name()
That looks much nicer to me than

Code:
for obj in objects:
    try:
        obj.method_name()
    except AttributeError():
        pass
That said, in some cases (i.e. when the attribute almost always exists on the objects) there can be performance benefits to the latter method.
Neko is offline   Reply With Quote
Old 07-22-2012, 01:22 AM   #621
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 13,918
Re: ** Python Support Thread **

+1 neko's answer

two other techniques that seem applicable:

- design your class structure to get rid of this problem:

Code:
class Parent(object):
    def updateDatabse(self):
        pass

class HasADatabase(Parent):
    def updateDatabase(self):
        # do real database-y things
- for data

Code:
attribute = getattr(klass, 'attribute')
if attribute is not None:
    # do the things
tyler_cracker is offline   Reply With Quote
Old 07-22-2012, 07:26 PM   #622
grinder
 
Join Date: May 2012
Location: Formerly Mariogs379
Posts: 461
Re: ** Python Support Thread **

Trying to find the smallest number divisible by each of the numbers between 1 and 20. Here's my attempt:

x = 1

y = 1

while y < 21:

if x % y == 0:
y = y + 1
else:
x = x + 1
y = 1

print(str(x))

Issue is, when I run it, Python doesn't print anything...not sure why. Ideas?
Mariogs37 is offline   Reply With Quote
Old 07-22-2012, 08:13 PM   #623
Carpal \'Tunnel
 
tyler_cracker's Avatar
 
Join Date: Apr 2005
Location: Shallow End OTKP
Posts: 13,918
Re: ** Python Support Thread **

use the [ code ] tags.

try putting a print inside your while loop so you can see what x and y are doing.
tyler_cracker is offline   Reply With Quote
Old 07-23-2012, 04:31 PM   #624
adept
 
Join Date: Apr 2010
Location: Deep end of the player pool
Posts: 733
Re: ** Python Support Thread **

Quote:
Originally Posted by Mariogs37 View Post
Trying to find the smallest number divisible by each of the numbers between 1 and 20. Here's my attempt:

x = 1

y = 1

while y < 21:

if x % y == 0:
y = y + 1
else:
x = x + 1
y = 1

print(str(x))

Issue is, when I run it, Python doesn't print anything...not sure why. Ideas?
Does the program exit? If it doesn't it means that y never reaches 21 and you have an infinite loop. I'm not a python developer but it looks to me like the print statement is not in the loop, given the indentation, so if you are stuck in the loop the print never executes.
myNameIsInga is offline   Reply With Quote
Old 07-23-2012, 04:57 PM   #625
grinder
 
Join Date: May 2012
Location: Formerly Mariogs379
Posts: 461
Re: ** Python Support Thread **

Should have used the code tags but have sorted out the issue, thanks guys.

On a dif not, why do the vowels below need quotes around them. I would have done it without...

Code:
secret = input("Type something amusing: ")

count_vowels = 0
count_vowel_pairs = 0

last_char_was_vowel = False


for each_letter in secret.lower():
    print(each_letter)
    if ( each_letter == "a"
         or each_letter == "e"
         or each_letter == "i"
         or each_letter == "o"
         or each_letter == "u" ) :

        count_vowels = count_vowels + 1
Midterm tomorrow so trying to iron these little things out.

Thanks again, you guys have been rly helpful!

-Mariogs
Mariogs37 is offline   Reply With Quote
Old 07-23-2012, 05:09 PM   #626
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,195
Re: ** Python Support Thread **

Code:
a
This is a variable named "a".

Code:
"a"
This is a string comprising the letter "a".
Xhad is online now   Reply With Quote
Old 07-23-2012, 05:25 PM   #627
veteran
 
Colombo's Avatar
 
Join Date: Feb 2005
Posts: 3,355
Re: ** Python Support Thread **

I am going to try to write a GUI program for my fantasy football leagues' live drafts this year.

My first though was to write it in javascript and run it on localhost, but I've never worked with python GUI stuff so I'd like to learn it.

Anyone have any experience/recommendations for which GUI package to use? It seems like there are many options - with TkInter being the stdlib one.

The program should be fairly simple and not very intensive. I'll simply have a form which holds all of the players and various information about them, a "DRAFT ME" button, and a second page/tab which lists the big board (where and to which team each player was drafted).
Colombo is offline   Reply With Quote
Old 07-23-2012, 07:05 PM   #628
grinder
 
Join Date: May 2012
Location: Formerly Mariogs379
Posts: 461
Re: ** Python Support Thread **

OK, so you'd only do that when comparing strings. When comparing integers, you wouldn't use quotes...yeah?
Mariogs37 is offline   Reply With Quote
Old 07-23-2012, 07:16 PM   #629
adept
 
Join Date: Apr 2010
Location: Deep end of the player pool
Posts: 733
Re: ** Python Support Thread **

Quote:
Originally Posted by Mariogs37 View Post
OK, so you'd only do that when comparing strings. When comparing integers, you wouldn't use quotes...yeah?
Yes. with no quotes you are comparing something to a variable. that is you are comparing something to the content in the variable. With the quotes you are not comparing to a variable but rather to a string. If you want to compare something with a particular integer you could simply type the integer (like 45), although that might not be good coding practice
myNameIsInga is offline   Reply With Quote
Old 07-23-2012, 07:21 PM   #630
Carpal \'Tunnel
 
Xhad's Avatar
 
Join Date: Jul 2005
Location: hatredcopter
Posts: 10,195
Re: ** Python Support Thread **

Code:
a = 5
b = "a"

a == 5 #True
"a" == 5 #False
a == "5" #False
b == 5 #False
b == a #False
b == "a" #True
Xhad is online now   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -4. The time now is 10:10 AM.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Copyright © 2008-2010, Two Plus Two Interactive