[Tutor] Crazy craps problem

Andreas Perstinger andreas.perstinger at gmx.net
Sun Oct 9 08:17:15 CEST 2011


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


More information about the Tutor mailing list