[Tutor] Sum of Scores

bhaaluu bhaaluu at gmail.com
Thu Jul 26 22:39:11 CEST 2007


Greetings,

Disclaimer: This source code is written by a Python Newbie.
                   Use at your own risk! =)

Here is a snippet that might work for one batter:

#!/usr/bin/env python
# cricket.py
# 2007-07-26
# b h a a l u u at g m a i l dot c o m
import random

def batterUp():
  score=[1,2,3,4,6,'Out']
  totalScore=0
  while 1:
    hit=random.choice(score)
    if hit != score[-1]:
      totalScore=totalScore+hit
      print "You batted",hit,"Total runs:",totalScore
    else:
      totalScore=totalScore+0
      print "You're OUT! Total runs:",totalScore
      break

batterUp()
# end criket.py

Notice that the list, score , has integers and a string in it.
I use the integers to add to the running score, and use the
string 'Out' to stop the while loop. I just did this, and it ran
okay the few times I tried it. YMMV. =)

Happy Programming!
-- 
bhaaluu at gmail dot com

On 7/26/07, Tony Noyeaux <tnoyeaux at msn.com> wrote:
>
>  The projects are moving along.
>
>  I've created a very simplistic cricket game to learn a few things.
>
>  The user is asked whether to play Aggressive or Defensively.
>
>  Once they pick... a random is picked based on their choice,.. and various
> scores happen until they get out.
>
>  Ignore the realism at this point,.. just getting the basic mechanics
> working first.
>
>  Works fine no problem.
>
>  I want to be able to calculate in the program,.. the total score,.. either
> at each successive score,... or when they finally get out. Not sure how to
> do that at this point.
>
>  So the output would look something like this.
>
>  You Hit a 2, you now have scored 2 runs
>  You hit a 4, you now have scored 6 runs
>  You hit a "Out", you are dismissed for 6 runs total.
>
>  After i get this worked out.. will tweak the actual formulas for the
> randoms, maybe put in multiple outs etc,.. changeable strategy at each out
> etc, and high scores list towards the end.
>
>  First things first,... and the project at hand.
>
>  How do i ... post the totals... as listed above, to the working code below.
>
>
>  Thanks as always
>
>  Tony Noyeaux
>
> ---------------------------------------------------------------------------------
>  import random
>  score = None
> strat = raw_input("Will you play (a)aggressive or (b)defensive?")
> if strat == "a":
>  while score != "Out":
>   score =
> random.choice(["1","2","3","4","4","6","6","Out","Out","Out","Out"])
>   print score
> else:
>  while score != "Out":
>   score =
> random.choice(["1","1","2","2","3","4","6","Out"])
>   print "You hit a "+score
> ----------------------------------------------------------------------------------
>
> ________________________________
> PC Magazine's 2007 editors' choice for best web mail—award-winning Windows
> Live Hotmail. Check it out!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


More information about the Tutor mailing list