[Tutor] Crazy craps problem

col speed ajarncolin at gmail.com
Sun Oct 9 07:16:51 CEST 2011


Hi again, Once more I've come up with a problem I can't explain. It must be
something simple, but I can't work it out.
The part of the script that is causing the problem is as follows:


def point(num):
    while True:
        raw_input("Roll")
        uno, dos = random.choice(dice), random.choice(dice)
        three = uno+dos
        print "{0} + {1} = {2}".format(uno, dos, three)
        print "Point is {0}. You scored {1}.".format(num, three)
        if three == num:
            return "win"
        if three == 7:
            return "lose"
        else:
            print "Try again."

What I have tried to do is - simulate dice throws, if the total is the same
as originally thrown, return from the function(this works). If I throw a 7,
I also want to return(this does not work as you can see from this sample
output:

Try again.
Roll
6 + 1 = 7
Point is 4. You scored 7.

<--> snip

4 + 1 = 5
Point is 4. You scored 5.
Try again.
Roll
3 + 4 = 7
Point is 4. You scored 7.
You have lost! You have $100 left.

As you can see, after throwing a 7, it just continues. It only returns after
throwing a second 7.

1. I know it's bad form to print from a function, I'll change it later.
2. I've tried the second if statement using "elif".
3. I've tried omitting the "three" variable and just using "uno" + "dos".
4. I've used "uno" and "dos" because I use "one" and "two" in another part
of the script(outside and after the function definition), although they
shouldn't be affected in any way.

Any help greatly appreciated.
Thanks
Col
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111009/37175ed7/attachment.html>


More information about the Tutor mailing list