Reputation: 33
We are trying to make a python program to simulate games of craps and display the probability of winning. I have narrowed down the answer to the while loop and as the title suggests, when it hits the loop it won't exit. As far as I can tell the loop should exit but instead just returns and endless train of "false". Here is the code for the whole program with comments, I'll type out the rules for craps (as far as what we're using for our program) at the bottom.
import random
def craps()
roll = random.randint(1,6) + random.randint(1,6)
if (roll == 7 or roll == 11): #Tests if the player should win. If so it adds a 1 to the win counter
return 1
elif (roll == 2 or roll == 3 or roll == 12): #Tests if player should lose. If so adds 0
return 0
else:
roll2 = 0 #initializes roll2 for the while
while (roll2 != roll or roll2 != 7): #The player keeps rolling until they get a 7 or the initial roll
roll2 == random.randint(1,6) + random.randint(1,6) #Rolls the dice
if (roll2 == roll): #Tests if the player should win
return 1
elif (roll2 == 7): #Tests if the player should lose
return 0
win = 0 #Start the win counter at 0
games = int(input("Enter the amount of games you want played: ")) #Accept how many games will be played
for i in range (games):
win = win + craps() #adds a 1 or a 0 depending on if a game was won
print ("The probability of winning is:", win, "/", games, " =", float(win)/games) #print the probability of winning
The basic rules we are using are: the player rolls 2 six sided dice, if the initial roll is a 7 or 11 the player wins (line 4). If the player rolls a 2, 3 or a 12 the player loses (line 6) If the player doesn't get either they keep rolling until they match the initial roll or roll a 7. If they match the initial roll they win, if they get a 7 they lose (lines 10-15). Our program is supposed to simulate and display the probability of the player winning.
This is my first time using this site so let me know if I screwed up on anything for the future. And thanks for helping!
Upvotes: 3
Views: 5348
Reputation: 2359
A common, but very frustrating and time-wasting typo, which can get even the best Python developers. Replace roll2 == random.randint(1,6) + random.randint(1,6)
with roll2 = random.randint(1,6) + random.randint(1,6)
.
Also, I think you need roll2 != roll and roll2 != 7
. From a stylistic point of view, it's better to omit the parenthesis around the statement evaluated in an if
or while
line.
Your loop has a slight amount of redundancy in it though. You check for roll2
being roll
or 7
at the top of each loop and at the end of each loop. You could also consider trying this:
while True:
# do everything in the same way as before
or
while roll2 != roll and roll2 != 7:
# do stuff with the roll
roll = random.randint(1,6) + random.randint(1,6)
return 1 if roll2 == roll else return 0 # a "ternary operator" in Python
Upvotes: 3
Reputation: 4129
Your first issue is with this line:
while (roll2 != roll or roll2 != 7): #The player keeps rolling until they get a 7 or the initial roll
This will be an infinite loop if roll is not equal to 7. You should have an and
here instead, because you want to change if one of these is false, not if both are. Make sense?
You want:
while (roll2 != roll and roll2 != 7): #as long as both are not `True` then keep rolling.
Your other issue is with this line:
roll2 == random.randint(1,6) + random.randint(1,6)
The double equals checks to see if roll2 is equal to the expression on the left. What you want to do is set it equal to the expression on the left, like so:
roll2 = random.randint(1,6) + random.randint(1,6)
Upvotes: 1
Reputation: 19264
A double equals sign tests for equality. Change it to single for your code to work. Here is your edited code:
import random
def craps()
roll = random.randint(1,6) + random.randint(1,6)
if (roll == 7 or roll == 11): #Tests if the player should win. If so it adds a 1 to the win counter
return 1
elif (roll == 2 or roll == 3 or roll == 12): #Tests if player should lose. If so adds 0
return 0
else:
roll2 = 0 #initializes roll2 for the while
while (roll2 != roll or roll2 != 7): #The player keeps rolling until they get a 7 or the initial roll
roll2 = random.randint(1,6) + random.randint(1,6) #Rolls the dice
if (roll2 == roll): #Tests if the player should win
return 1
elif (roll2 == 7): #Tests if the player should lose
return 0
win = 0 #Start the win counter at 0
games = int(input("Enter the amount of games you want played: ")) #Accept how many games will be played
for i in range (games):
win = win + craps() #adds a 1 or a 0 depending on if a game was won
print ("The probability of winning is:", win, "/", games, " =", float(win)/games) #print the probability of winning
Here is an example:
>>> import time
>>> x = 7
>>> x == 7
True
>>> while x != 6:
... x == 6
... time.sleep(1)
...
False
False
False
^CTraceback (most recent call last):
File "<stdin>", line 3, in <module>
KeyboardInterrupt
>>> while x != 6:
... x = 6
... time.sleep(1)
...
>>>
Upvotes: 2