Can someone tell me why my program doesn't work? Even when I set s as 10000.
When I printed out wins and plays it just shows up as 0 and 1.
def manyCraps(s):
wins = 0
plays = 0
dice1 = randint(1,6)
dice2 = randint(1,6)
total = dice1 + dice2
for i in range(s):
if total == 7 or total == 11:
wins = wins + 1
plays = plays + 1
else:
if total == 2 or total == 3 or total == 12:
plays = plays + 1
else:
plays = plays + 1
dice3 = randint(1,6)
dice4 = randint(1,6)
total = dice3 + dice4
Related
Here is the game: the player throws a pair of standard, six-sided dice.
If the player rolls a total of 7 or 11, the player wins.
If the player rolls a total of 2, 3, or 12, the player loses.
For all other roll values, the player will repeatedly roll the pair of dice until either she rolls the initial value again (in which case they win) or 7 (bet is lost.)
Here is my code:
from random import seed, randint
def simulate():
win_count = 0
die1 = randint(1, 6)
die2 = randint(1, 6)
roll = die1 + die2
if roll == 7 or roll == 11:
return True
win_count += 1
elif roll == 2 or roll == 3 or roll == 12:
return False
else:
die1 = randint(1, 6)
die2 = randint(1, 6)
new_roll = die1 + die2
while roll != new_roll and roll != 7:
if new_roll == roll:
return True
win_count += 1
elif new_roll == 7:
return False
return win_count
## Main
for trials in range(1000):
simulate()
print(win_count / 1000)
I also need to find the probability of winning the "pass bet", which is what the rules are described above. The number of trials is 1000. However, nothing returns from this code. Where am I going wrong?
I'm a beginner and I can't figure out why I can't get the output I
want. It's a craps game. It's suppose to go like:
How many games do you want to play > 6 You rolled 5 + 2 = 7 You
win
What I got is something like: You rolled 1 + 6 = 7 You rolled 1 + 6 =
7 You rolled 1 + 6 = 7 You lose
import random
def rollDice():
roll_1 = random.randint(1,7)
roll_2 = random.randint(1,7)
return roll_1, roll_2
def determine_win_or_lose(dice1,dice2):
sum = dice1 + dice2
print("You rolled", dice1, "+", dice2, "=", sum )
if sum == '2' or '3' or '12':
return 0
elif sum == '7' or '11':
return 1
else:
print("Point is", sum)
determinePointValueResult(sum)
if determinePointValueResult(sum) == 1:
return 1
elif determinePointValueResult(sum) == 0:
return 0
def determinePointValueResult(sum):
point = sum
while sum != 7 and sum != point:
x, y = rollDice()
sum = x + y
if sum == point:
return 1
elif sum == '7':
return 0
print("You rolled", x, "+", y, "=", sum )
#==== MAIN =====
win = 0
lose = 0
game = int(input("How many games do you want to play > "))
for i in range(game):
x, y = rollDice()
determine_win_or_lose(x, y)
if determine_win_or_lose(x, y) == 1:
print("You win")
win = win + 1
elif determine_win_or_lose(x, y) == 0:
print("You lose")
lose = lose + 1
print("Game results: ", win, "wins and", lose, "losses")
Your issue come from the main, because you call the determine_win_or_lose function 3 times, the first one before the if (and i'm not sure why since you do nothing with it), a second time to check the condition of the if and a third time to check the condition of the elif.
Since it's this function that print the message, and you call the function 33 times each iteration of the for loop, it's normal to get the message printed 3 times.
(
Also since the determine_win_or_lose will always return 0 or 1 you don't really need an elif you can just do an if/else to achieve the same thing and simplify your code a bit.
)
So i'd suggest the following :
#==== MAIN =====
win = 0
lose = 0
game = int(input("How many games do you want to play > "))
for i in range(game):
x, y = rollDice()
result = determine_win_or_lose(x, y)
if result == 1:
print("You win")
win = win + 1
else:
print("You lose")
lose = lose + 1
print("Game results: ", win, "wins and", lose, "losses")
Obvious issues:
You call determine_win_or_lose too many times in your for loop. Change it to:
for i in range(game):
x, y = rollDice()
result = determine_win_or_lose(x, y)
if result == 1:
print("You win")
win = win + 1
elif result == 0:
print("You lose")
lose = lose + 1
Your check in determine_win_or_lose is incorrect. It should be something like:
def determine_win_or_lose(dice1,dice2):
sum = dice1 + dice2
print("You rolled", dice1, "+", dice2, "=", sum )
if sum == 2 or sum == 3 or sum == 12:
return 0
elif sum == 7 or sum == 11:
return 1
else:
print("Point is", sum)
determinePointValueResult(sum)
if determinePointValueResult(sum) == 1:
return 1
elif determinePointValueResult(sum) == 0:
return 0
In determinePointValueResult you shouldn't compare sum to a string, but an integer:
def determinePointValueResult(sum):
point = sum
while sum != 7 and sum != point:
x, y = rollDice()
sum = x + y
if sum == point:
return 1
elif sum == 7:
return 0
print("You rolled", x, "+", y, "=", sum )
It's possible that determine_win_or_lose and determinePointValueResult are returning None. You may need to change your elifs to elses or create a new else case.
This is the piece of code and when I run the code nothing is outputted when the outputs should be the dice totals.Can somebody please help me identify the error
import random
r1 = random.randint(1,6)
r2 = random.randint(1,6)
r3 = random.randint(1,6)
def ScoreDice(Dice1 , Dice2 , Dice3):
Dice1 = r1
Dice2 = r2
Dice3 = r3
if Dice1 == Dice2 == Dice3:
total = Dice1 + Dice2 +Dice3
print(total)
elif Dice1 == Dice2:
total = Dice1 + Dice2 - Dice3
print(total)
elif Dice2 == Dice3:
total = Dice3 + Dice2 - Dice1
print(total)
elif Dice1 == Dice3:
total = Dice3 + Dice1 - Dice2
print(total)
else:
total = 0
print(total)
import random
r1 = random.randint(1,6)
r2 = random.randint(1,6)
r3 = random.randint(1,6)
def ScoreDice(Dice1 , Dice2 , Dice3):
if Dice1 == Dice2 == Dice3:
total = Dice1 + Dice2 +Dice3
print(total)
elif Dice1 == Dice2:
total = Dice1 + Dice2 - Dice3
print(total)
elif Dice2 == Dice3:
total = Dice3 + Dice2 - Dice1
print(total)
elif Dice1 == Dice3:
total = Dice3 + Dice1 - Dice2
print(total)
else:
total = 0
print(total)
ScoreDice(r1,r2,r3)
I guess you're new to coding... I suggest you work on logic and syntaxes first, before posting your questions.
I'm trying to write a count function, to calculate the number of rolls it takes to hit 0. And it is returning me the random integer of the diceroll + 1, how do I get it to count the number of times the myroll variable occurs.
import random
def luckysevens():
mypot = int(input("Please enter the amount of money you want to in the pot: "))
while mypot > 0:
diceroll = random.randint(1, 6)
print(diceroll)
myroll = (diceroll + diceroll)
if myroll == 7:
mypot = mypot + 4
print("Your roll was a 7 you earned 4$", mypot)
else:
mypot = mypot - 1
print("Your roll was", myroll, "you lost 1$", mypot)
if mypot == 0:
print("Your out of money!")
sum = 0
for count in range(myroll + 1):
sum = sum + count
print(count)
luckysevens()
If you want to count how many rolls before the loop exits, simply add another counter variable. I also am assuming you're rolling a couple dice, so added different random calls for each one.
import random
mypot = int(input("Please enter the amount of money you want to in the pot: "))
num_rolls = 0
while mypot > 0:
die_1 = random.randint(1,6)
die_2 = random.randint(1,6)
myroll = die_1 + die_2
num_rolls += 1 # count rolls
if myroll == 7:
mypot = mypot + 4
print("Your roll was a 7 you earned 4$",mypot)
else:
mypot = mypot - 1
print("Your roll was",myroll,"you lost 1$",mypot)
if mypot == 0:
print("Your out of money!")
print 'Num rolls: {}'.format(num_rolls) # print rolls
I'm a beginner, and I wanted to know if there was a simpler way of write this out in Python. I'm assuming some type of dictionary, but I do not understand how to write it out.
I was on a cruise a couple of days ago, and I play craps. I wanted to know if the odds are somewhat correct. So, I wrote this, but I know there is a simpler way.
import random
dice2 = 0
dice3 = 0
dice4 = 0
dice5 = 0
dice6 = 0
dice7 = 0
dice8 = 0
dice9 = 0
dice10 = 0
dice11 = 0
dice12 = 0
for i in range(100000):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
number = dice1 + dice2
#print(dice1)
if number == 2:
dice2 +=1
elif number == 3:
dice3 += 1
elif number == 4:
dice4 += 1
elif number == 5:
dice5 += 1
elif number == 6:
dice6 += 1
elif number == 7:
dice7 += 1
elif number == 8:
dice8 += 1
elif number == 9:
dice9 += 1
elif number == 10:
dice10 += 1
elif number == 11:
dice11 += 1
elif number == 12:
dice12 += 1
total = dice2+dice3+dice4+dice5+dice6+dice7+dice8+dice9+dice10+dice11+dice12
At the end of this, it just prints out the percentage of hits on numbers from 2-12.
I'd use Counter, as that's what it was made for:
from random import randint
from collections import Counter
counts = Counter(randint(1, 6) + randint(1, 6) for i in range(100000))
total = sum(counts.values())
number_of_tens = counts[10]
from random import randint
dice = [0]*11
for i in range(100000):
dice[randint(1,6)+randint(1,6)-2] += 1
total = sum(dice) #it is 100000, of course
for i, v in enumerate(dice, 2):
print('{0}: {1}%'.format(i, v*100.0/total))
import random
def roll(n=6):
return random.randint(1, n)
dice = dict.fromkeys(range(2, 13), 0)
for i in range(100000):
number = roll() + roll()
dice[number] += 1
total = float(sum(dice.values()))
for k,v in dice.items():
print "{}, {:.2%}".format(k, v/total)