[Tutor] Separating recursion, for loops, and while loops
Guess Who? Me
beercanz@hotmail.com
Thu Jan 23 00:22:02 2003
I started working on my dice program again, only this time I've decided to
split it up - I want to eventually make three entirely separate ways to do
the same thing (just for practice's sake) - one with a 'with' loop, one with
a 'for' loop, and one with recursion. I have a question about the
'referencing local variable total before assignment', and I'd like some
input (ha ha - no pun intended) on what I've done so far, if there is
anything I should be doing differently, etc. (The program isn't finished,
only the first part of it works, and the second part of it gives me that
strange referencing error, even when I put total=0 at the top...well anyway,
here is the code).
################
#Dice Roll Program
import random
#Use for loops, while loops, and recursion, separately, to yield
#a dice program.
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."
while rolls > 0:
value=random.randint(1,sides)
rolls=rolls-1
print value
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."
# I had a problem using rolls because it wasn't in a form that the
#for list could swallow, I think, so I made it use the output of the #range
#function.
rolls=range(1,rolls)
for elem in rolls:
elem=random.randint(1,sides)
value=elem
total=value+total
#'local variable total referenced before assignment'
print total
else:
print "The end."
choice=input("Press 0 to try my (working) while loop code,"\
"press 1 to use the for loop code:")
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)
###################
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?
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.
Oh, and the 'for' statement wouldn't work when I would use the input 'rolls'
as a number - was that just in my imagination?
http://www.python.org/doc/current/ref/for.html says it can 'iterate over'
the following: string, tuple or list. Why would you 'iterate' over a string
- isn't a single string just one thing? Or does it mean that it can do
operations on a string, and therefore can do operations on a string in a
list? Why would you use the for statement for that? Also, what in the world
is a tuple?
Well...I'm done. I hope the questions aren't too horrible.
Thank you for all of the previous help, (I think its starting to show a
little! WOOHOO!)
~Travis
_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963