[Tutor] Crazy craps problem

col speed ajarncolin at gmail.com
Sun Oct 9 08:25:48 CEST 2011


On 9 October 2011 13:17, Andreas Perstinger <andreas.perstinger at gmx.net>wrote:

> On 2011-10-09 07:16, col speed wrote:
>
>> 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:
>>
>
> I'm pretty sure that your problem is not in the code snippet you have shown
> us. Here it works as expected (I've copied your code, added "import random"
> and "dice = [1, 2, 3, 4, 5, 6]" at the top and saved as "dice.py"):
>
> Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
> [GCC 4.5.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import dice
> >>> dice.point(1)
> Roll
> 4 + 5 = 9
> Point is 1. You scored 9.
> Try again.
> Roll
> 4 + 3 = 7
> Point is 1. You scored 7.
> 'lose'
> >>>
>
> Please show us the part where you use the "point" function.
>
> Bye, Andreas
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>


Thanks for your prompt reply! Here's the whole thing:

import random

message = """Welcome to craps!!!! Place your bet
             and roll the dice.
             7 or 11 wins.
             2, 3 or 12 loses.
             Others are "point"."""
player = "Your"
dice = range(1, 7)
stake = 100
bet = 5
winmsg = "You have won!You have ${0} left.".format(stake)
losemsg = "You have lost! You have ${0} left.".format(stake)
players = ["Your", "My"]

def win(num):
    if num in [7,11]:
        return "win"
    elif num in [2,3,12]:
        return "lose"
    else:
        return "point"

def changePlayer(player):
    if player == "Your":
        return "My"
    else:
        return "Your"

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."

print message
while stake:
    print "{0} throw! You have ${1}. How much do you bet?".format(player,
stake)
    bet = int(raw_input("$"))
    stake -= bet
    one, two = random.choice(dice), random.choice(dice)
    print "{0} + {1} = {2}".format(one, two, one+two)
    if win(one+two) == "win":
        stake += bet*2
        print winmsg
    elif win(one+two) == "lose":

        print losemsg
    else:
        if point(one+two) == "win":
            stake += bet*2
            print winmsg
        elif  point(one+two) == "lose":

            print losemsg
            player = changePlayer(player)

    Thanks
Col
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111009/1f8dc49b/attachment.html>


More information about the Tutor mailing list