[Tutor] Separating recursion, for loops, and while loops
Jayprasad J. Hegde
jjhegde@konark.ncst.ernet.in
Thu Jan 23 02:11:48 2003
hello "Guess who?",
I have modified your program to make it work for
"while", "for" and the recursive case.
All the three have a similar behaviour, unlike the original version, and print
out the dice "values" before terminating by printing out the cumulative values
("total").
problem with "while" version:
_no_ return value; you are using "print while_dice..." in the main portion of the code
My version: returns the total
problem with the "for" version:
1. _no_ return value
2. redundancy for a short code e.g.
> elem=random.randint(1,sides)
> value=elem.
I guess, if it helps you understand better you can stick to it.
3. total used on the RHS before "declaring". To avoid this problem
you need to say "total = ..." before this statement.
I have created a recursive version for you, in case you need some reference.
Its open to all for comments.
>
> And one more thing - the first part, loop_dice, outputs things on different
> lines - is there any way, without using recursion, to add all the numbers
> together before printing them?
you mean _juxtapositioning_ (or putting things on the same line) and
not _addition_ don't you? In that case simply put a comma at the end of
the print statement e.g. "print value,"
>
> And if I am using recursion, same question - can I output add the numbers
> together and output them? I'm sure I can, I just don't know how, and would
> appreciate the enlightenment.
check out the code. :)
You'll find the code below my "signature" section.
regards
- JJH
ps - why the anonymity?
--
Human beings were created by water to transport it uphill.
(defun JJHdetails ()
(format t "~&~A~&~A~&~A"
"Jayprasad J Hegde, Staff Scientist, KBCS, NCST" "Gulmohar Cross
Road 9, Juhu, Mumbai 400049." "tel: +91-22-26201606x373"))
-------------------
import random
def loop_dice(rolls, sides):
if rolls<1:
print "If you don't play, you can't win or lose."
elif sides<1:
print "There are either 0 sides, or infinitely many sides. Trippy."
total = 0
while rolls > 0:
value=random.randint(1,sides)
rolls=rolls - 1
print value,
total = total + value
print
return total
def for_dice(rolls, sides):
if rolls<1:
print "If you don't play, you can't win or lose."
elif sides <1:
print "There are either 0 sides, or infinitely many sides. Trippy."
total = 0
for elem in range (rolls):
value=random.randint(1,sides)
total= value + total
#'local variable total referenced before assignment'
print value,
print
return total
def rec_dice (myrolls, mysides):
total = 0
if (myrolls >= 0):
value = random.randint (1, mysides)
print value,
total = rec_dice (myrolls - 1, mysides) + value
return total
choice=input(" 0 == while, 1 == for, 2 == rec:")
rolls=input("How many rolls would you like to make?")
sides=input("How many sides would you like to make?")
if choice == 0:
print loop_dice(rolls,sides)
elif choice == 1:
print for_dice(rolls,sides)
elif choice == 2:
print
print rec_dice (rolls, sides)