another beginner game question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hamy Li
    New Member
    • Mar 2008
    • 15

    another beginner game question

    Hi all

    Its me again. This time I want to create a program that flips a coin 100 times
    and tells you the number of heads and tails

    I tried using an intentional infinite loop but it didn't turn out right
    please give some advice

    thanks
  • elcron
    New Member
    • Sep 2007
    • 43

    #2
    Originally posted by Hamy Li
    Hi all

    Its me again. This time I want to create a program that flips a coin 100 times
    and tells you the number of heads and tails

    I tried using an intentional infinite loop but it didn't turn out right
    please give some advice

    thanks
    Can you post some code. Use a for loop for the loop.
    [code=python]
    def flipCoin():
    #...

    for i in xrange(100):
    print flipCoin()
    [/code]

    Comment

    • Hamy Li
      New Member
      • Mar 2008
      • 15

      #3
      Originally posted by elcron
      Can you post some code. Use a for loop for the loop.
      [code=python]
      def flipCoin():
      #...

      for i in xrange(100):
      print flipCoin()
      [/code]


      Well I first tried to make it print out the 100 times of coin flip

      Code:
      import random
      tries = 1
      while True:
          side = random.randrange(2)
          tries += 1
          if tries > 100:
              break
      
      if side == 0:
          print "Heads"
      elif side == 1:
          print "tails"
      else:
          print "None"
      print side
      
      
      raw_input("\n\nPress the Enter key to exit")

      but it didn't turn out so well

      Comment

      • bgeddy
        New Member
        • Aug 2007
        • 16

        #4
        Well going through your code - here are some observations.

        Firstly you do not accumulate the numer of heads and tails occurring so you have no count to show. Secondly, as stated, if you are simply doing something a set number of times in a loop a 'for num in xrange(loops)'
        is the simplest construct.

        Even if using a while loop as you have, you don't need an infinite loop and a break, in your case "while tries<=100" would be better. Then you drop the indentation so only the last side tossed will be printed and as side can only be 0 or 1 you don't need the 'else : print 'None'.

        The code here may give you some ideas - I've included a dictionary, a tuple and a subscript and an augmented assignment when they're not strictly necessary to give you some things to check up on !

        Code:
        import random
        tosses={"Heads":0,"Tails":0}
        for i in xrange(100):
            tosses[("Heads","Tails")[random.randrange(2)]]+=1
        print tosses
        Hope this helps..

        Comment

        • Hamy Li
          New Member
          • Mar 2008
          • 15

          #5
          thanks alot for you time.

          and what does xrange do?

          Comment

          • jlm699
            Contributor
            • Jul 2007
            • 314

            #6
            Originally posted by Hamy Li
            and what does xrange do?
            Same thing as range() only instead of returning a list it's more of an as-needed int ... it's better for memory efficiency and faster than range in loops..

            Comment

            • bgeddy
              New Member
              • Aug 2007
              • 16

              #7
              I'm not sure what is this site's policy on giving URL's so I'll just say if you were to google "python411" you will find a site with loads of tutorials/books/links etc for python.

              Read up as much as you can but always have a python interpreter fired up to play with ! You will learn much more by experimentation of concepts.

              At the python prompt you can type :

              Code:
              help(xrange)
              to get an idea what this does (althought you may find the information cryptic at first).

              Good Luck !!

              Comment

              • Hamy Li
                New Member
                • Mar 2008
                • 15

                #8
                thanks alot. The website sure helped me

                Comment

                Working...