Re: [Tutor] Rock, Paper, Scissors
> My main concern is the compare_objects function. Is > there any easier way to write it? Actually, the > function does not work because "else condition:" > causes a syntax error. I'd do that with a two dimernsional table. The table would be indexed by the computers choice and the human choice and the outcome stored as a value. Thus it becomes outcome = results[computer.choice][human.choice] if outcome == 0: print "Computer Wins" elif outcome == 1: print "Human wins" else: print 'Draw!' Another interesting variation is to create a single Player class and make human and computer instances. Pass in the function for setting choice as a parameter to init... That way your code consists of a Player class, two selection functions and the lookup/display code that could sit in main() This isn't any "better", as such, just different... HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Block
I'm really blocked right now, and I don't want to post this to the main Python list since I think the answer is probably right in front of my face. I'm clearly just too close to see it. I'm able to do frequency counts of items in a list by doing the following: >>> list = ["5100", "5100", "5100", "5200", "5200"] >>> count = {} >>> for l in list: ... count[l] = count.get(l, 0) + 1 ... >>> count {'5200': 2, '5100': 3} >>> Hooray, right?! However, my real problem is dealing with another dimension of data. So suppose I can access the following data: "5100", "foo" "5100", "-" "5100", "-" "5100", "-" "5200", "foo" "5200", - That's a total of 6 line records that I want to output like this: 5100- 3 5100 foo 1 5200 - 1 5200 foo 1 I want to count the frequency of the second column of data that I can access. I can't seem to get any traction as how to figure this out. Should I put the data in a list of dictionaries? A dictionary of lists? How do I call the data if there are all kinds of duplicate keys? When the data's in some data structure how do I iterate over it to get frequency of column B? Really confused. Can someone help? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Rock, Paper, Scissors
the else part can't have a condition!!! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Rock, Paper, Scissors
Robert Wierschke wrote: > the else part can't have a condition!!! > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > Are you referring to Alan's post? You should have included the original text in your reply so we would know what you're talking about. If you _are_ talking about Alan's example: #--- outcome = results[computer.choice][human.choice] if outcome == 0: print "Computer Wins" elif outcome == 1: print "Human wins" else: print 'Draw!' #--- his else doesn't have a condition. it's just that he only needed to do one thing in the else clause and it looks neater to put it on one line. else: print 'Draw!' is equivalent to else: print 'Draw!' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Rock, Paper, Scissors
> This looks like a fun project to work on. From > reading the description, I feel this would be pretty > straight forward game to program. However, I have no > idea how the computer would decide if it wanted a > rock, paper, or a pair of scissors. Any hints? christopher, this is indeed a *great* exercise... i've been using it since i can't remember when... i had it as an exercise in BASIC and Pascal when i was in high school, then turned it into an exercise for my C and Python courses. naturally it's also in Core Python. others have said it and given code already, but this problem breaks down into 3 discrete steps: 1. give values to R, P or S, i.e., an int, char, or some constant 2. have the user choose one via its constant 3. have the computer choose another -- you will need to randomly pick one of the 3 constants 4. logic to determine who the "winner" is of if it is a draw good luck! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Block
On 8/11/06, Terry Peppers <[EMAIL PROTECTED]> wrote: > I'm really blocked right now, and I don't want to post this to the > main Python list since I think the answer is probably right in front > of my face. I'm clearly just too close to see it. > > "5100", "foo" > "5100", "-" > "5100", "-" > "5100", "-" > "5200", "foo" > "5200", - > > That's a total of 6 line records that I want to output like this: > > 5100- 3 > 5100 foo 1 > 5200 - 1 > 5200 foo 1 > > I want to count the frequency of the second column of data that I can > access. I can't seem to get any traction as how to figure this out. > Should I put the data in a list of dictionaries? A dictionary of > lists? How do I call the data if there are all kinds of duplicate > keys? When the data's in some data structure how do I iterate over it > to get frequency of column B? without hacking any code... i can see two possibilities you can try: 1) easiest. use a tuple of pairs as your key, i.e. (5100, 'foo'), (5100, '-'), etc. it's only confusing because you're seeing "5100" in both keys 2) more complex. you suggested a list of dicts or a dict of lists, but it looks like a dict of dicts would be a better fit., i.e. {5100: {'foo': 1, '-': 3}, etc. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rock, paper, scissors
--- Tom Wilson <[EMAIL PROTECTED]> wrote: > hi, > > could you please explain to me how your rock paper > scissors game script > works because i am a bit confused. > > thanks > tom > > The game does not work in its current form, which may be some cause for confusion. :-) In designing the program, I took the object oriented route. Why? When I started to design the game, I imagined two characters (a human and a computer) playing against each other. Each character has similar attributes, i.e. points and a choice, and behavoir. They play. The behavoir is implemented differently for each character because how a computer makes a decision is different from how a human makes a decision. After the characters decide which object to pick, a function called compare_objects compares the objects and uses logic to determine the winner. After the winner is picked, points are incremented and play continues until one character reaches a set number of points. The two problem areas are the compare_objects function and the while loop. They don't work. (At least, not well.) However, I have gotten several useful tips from tutors, so I will be making a lot of progress next week! -Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Rock, Paper, Scissors
Luke Paireepinart escribió: > Robert Wierschke wrote: > >> the else part can't have a condition!!! >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> >> > Are you referring to Alan's post? > You should have included the original text in your reply so we would > know what you're talking about. He wasn't. From the original post: elif human.choice == 'rocks' and computer.choice == 'scissors': print "Human wins!" human.points = human.points + 1 else human.choice == 'scissors' and computer.choice == 'rocks': print "Computer wins!" computer.points = computer.points + 1 Ismael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Rock, Paper, Scissors
> He wasn't. From the original post: > > elif human.choice == 'rocks' and computer.choice == > 'scissors': > print "Human wins!" > human.points = human.points + 1 > else human.choice == 'scissors' and computer.choice > == 'rocks': > print "Computer wins!" > computer.points = computer.points + 1 > > > ah. thanks for clearing this up. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor