Tags: coin, flips, heads, loop, number, program, programming, python, range, tells, toss, write

coin toss program without using the for/range loop

On Programmer » Python

4,901 words with 4 Comments; publish: Mon, 05 May 2008 23:45:00 GMT; (20078.13, « »)

How to write the coin toss program without using the for/range loop program.

Program flips a coin 100 times and then tells you the number of heads and

tails.

I can only solve it using the for/range loop

Import random

Heads=0

For 1 in range (100):

Heads+=random.randrange(2)

print "Hit heads"+" "+str(heads)+" "+"times"+" " + "hit tails" + "

"+str(100-heads)+" " + "times"

I don't see how you solve this program just using the while loop program and

if/else statement.

Tutor maillist - Tutor (AT) python (DOT) org

All Comments

Leave a comment...

  • 4 Comments
    • David wrote:

      How to write the coin toss program without using the for/range loop program.

      Program flips a coin 100 times and then tells you the number of heads

      and tails.

      I can only solve it using the for/range loop

      Import random

      Heads=0

      For 1 in range (100):

      Heads+=random.randrange(2)

      print Hit heads+ +str(heads)+ +times+ + hit tails +

      +str(100-heads)+ + times

      I dont see how you solve this program just using the while loop program

      and if/else statement.

      This sounds a lot like homework so I won't give you the whole answer,

      but you can write a for loop using while and a counter variable.

      Kent

      Tutor maillist - Tutor (AT) python (DOT) org

      #1; Mon, 05 May 2008 23:46:00 GMT
    • David wrote:

      How to write the coin toss program without using the for/range loop program.

      Program flips a coin 100 times and then tells you the number of heads

      and tails.

      I can only solve it using the for/range loop

      Import random

      Heads=0

      For 1 in range (100):

      Heads+=random.randrange(2)

      print Hit heads+ +str(heads)+ +times+ + hit tails +

      +str(100-heads)+ + times

      I dont see how you solve this program just using the while loop program

      and if/else statement.

      This sounds a lot like homework so I won't give you the whole answer,

      but you can write a for loop using while and a counter variable.

      Kent

      Tutor maillist - Tutor (AT) python (DOT) org

      #2; Mon, 05 May 2008 23:48:00 GMT
    • Kent Johnson wrote:

      David wrote:

      >How to write the coin toss program without using the for/range loop program.

      >>

      >Program flips a coin 100 times and then tells you the number of heads

      >and tails.

      >>

      >

      >>

      >I can only solve it using the for/range loop

      >>

      >

      >>

      >Import random

      >>

      >Heads=0

      >>

      >For 1 in range (100):

      >>

      >Heads+=random.randrange(2)

      >>

      >

      >>

      >print Hit heads+ +str(heads)+ +times+ + hit tails +

      >+str(100-heads)+ + times

      >>

      >

      >>

      >I dont see how you solve this program just using the while loop program

      >and if/else statement.

      >

      >

      This sounds a lot like homework so I won't give you the whole answer,

      but you can write a for loop using while and a counter variable.

      In fact, in most languages a for loop and a while loop are very similar:

      for(int i =0; i < 100; i++)

      {

      //do something

      }

      is the same as

      int i = 0

      while (i < 100)

      {

      // do something

      i++;

      }

      The difference in Python comes from the fact that the 'for' loop

      iterates over a list of objects,

      rather than incrementing a variable.

      If you're using a for-range loop in Python you're using it in the

      old-style that C and C++ programs use (other languages too)

      in which case you could easily use a while loop as well.

      But if you're indexing into a specific list,

      it becomes much more clear why we use python's 'for',

      to index directly into that list without having to deal with an index

      variable.

      for x in some_list:

      // do something with x

      rather than

      index = 0

      while index < len(some_list):

      x = some_list[index]

      // do something with x

      index += 1

      HTH,

      -Luke

      Kent

      Tutor maillist - Tutor (AT) python (DOT) org

      Tutor maillist - Tutor (AT) python (DOT) org

      #3; Mon, 05 May 2008 23:48:00 GMT
    • Kent Johnson schrieb:

      David wrote:

      >print Hit heads+ +str(heads)+ +times+ + hit tails +

      >+str(100-heads)+ + times

      This sounds a lot like homework []

      An if you want to get extra marks for pythonicity ;-), read about string

      formatting here:

      Chris

      Tutor maillist - Tutor (AT) python (DOT) org

      #4; Mon, 05 May 2008 23:50:00 GMT