Program iterates through the first roll of Craps, which is supposed to output corresponding text and pictures based on each random roll.
from random import randint
from PIL import Image
"""
You are going to code the game of craps.
_____________________________________________________________________________
1. You will create 2 dice to roll. You will include a graphic of the dice for each roll.
2. On the first roll, if the dice total a 7 or 11 you win, print to the screen “You win",
3. If the dice total 2, 3, 12 you lose, print to the screen "You lose".
4. Any other number you keep rolling until you win or lose.
5. You can include a loop to prompt for ending or continuing the game so you do not have to rerun the program.
6. When you exit the game, print to the screen the number of wins and loses.
A return value of 1 means you win, a return value of 0 means you lost. You can use a different variable to keep track of your wins and loses.
"""
roll = []
wins = 0
losses = 0
d1 = 0
d2 = 0
rollResults = 0
dieOne = Image.open('dieOne.jpeg')
dieTwo = Image.open('dieTwo.jpeg')
dieThree = Image.open('dieThree.jpeg')
dieFour = Image.open('dieFour.jpeg')
dieFive = Image.open('dieFive.jpeg')
dieSix = Image.open('dieSix.jpeg')
def getRollFaces(d1,d2):
### RETURNS OUR DICE GRAPHIC FOR EACH ROLL ###
dieOne = Image.open('dieOne.jpeg')
dieTwo = Image.open('dieTwo.jpeg')
dieThree = Image.open('dieThree.jpeg')
dieFour = Image.open('dieFour.jpeg')
dieFive = Image.open('dieFive.jpeg')
dieSix = Image.open('dieSix.jpeg')
faceOne = None
faceTwo = None
if d1 == 1:
faceOne = dieOne.show()
if d1 == 2:
faceOne = dieTwo.show()
if d1 == 3:
faceOne = dieThree.show()
if d1 == 4:
faceOne = dieFour.show()
if d1 == 5:
faceOne = dieFive.show()
if d1 == 6:
faceOne = dieSix.show()
if d2 == 1:
faceTwo = dieOne.show()
if d2 == 2:
faceTwo = dieTwo.show()
if d2 == 3:
faceTwo = dieThree.show()
if d2 == 4:
faceTwo = dieFour.show()
if d2 == 5:
faceTwo = dieFive.show()
if d2 == 6:
faceTwo = dieSix.show()
return faceOne,faceTwo
def roll_dice():
wins = 0
losses = 0
roll = []
d1 = randint(1, 6)
roll.append(d1)
d2 = randint(1, 6)
roll.append(d2)
rollResults = d1 + d2
getRollFaces(d1,d2)
print("Your roll is: {}".format(rollResults))
if rollResults == 2 or rollResults == 3 or rollResults == 12:
losses = losses + 1
loss = input("We are sorry for your loss. :( Play again? Y or N ")
if loss == "Y" or loss == "y":
roll_dice()
elif loss == "N" or loss == "n":
print("Total wins: {}. Total losses: {}. Thanks for playing!".format(wins,losses))
return 0
elif rollResults == 7 or rollResults == 11:
wins += 1
win = input("Congrats on your win! :) Play again? Y or N ")
if win == "Y" or win == "y":
roll_dice()
elif win == "N" or win == "n":
print("Total wins: {}. Total losses: {}. Thanks for playing!".format(wins,losses))
return 1
else:
roll_dice()
roll_dice()
As mentioned in the title, my images are not displaying on my console as part of my output. My wins/losses accumulator is also not working properly.
PIL documentation was consulted prior to posting.
Please advise!
Related
I am making this kind of a game but it isn't really a game so basically I want this to run every time I hit space but it doesn't work no matter what I try so I would be really thankful if somebody could have helped me out on this.
import random
import keyboard
food = 5
x = 0
y = 0
if keyboard.is_pressed('space'):
bobsDecision = random.randint(0,1)
if bobsDecision == 1:
print ('bob ate')
food = 5
else:
xoy = random.randint(1,4)
if xoy == 1:
x = x + 1
elif xoy == 2:
x = x - 1
elif xoy == 3:
y = y + 1
elif xoy == 4:
y = y - 1
food = food - 1
print ('the cords are ', x, " ", y)
print ('The food supply is ', food)
Your code runs once and immediately skips over keyboard.is_pressed("space"), and then exits.
What you want to do instead is to loop forever, and use the keyboard module's read_key functionality to make it wait for a keypress.
An example of this is this - I also added support for exiting the loop/game with esc.
import random
import keyboard
food = 5
x = 0
y = 0
while True:
keyboard.read_key()
if keyboard.is_pressed("esc"):
print("Stopping play...")
break
elif keyboard.is_pressed("space"):
bobsDecision = random.randint(0,1)
if bobsDecision == 1:
print ('bob ate')
food = 5
else:
xoy = random.randint(1,4)
if xoy == 1:
x = x + 1
elif xoy == 2:
x = x - 1
elif xoy == 3:
y = y + 1
elif xoy == 4:
y = y - 1
food = food - 1
print ('the cords are ', x, " ", y)
print ('The food supply is ', food)
You need to put the if statement in a while loop. But ALSO be sure to have some kind of exit code. Below, I used the keypress esc to stop the while loop:
import random
import keyboard
food = 5
x = 0
y = 0
while True:
keyboard.read_key() # an important inclusion thanks to #wkl
if keyboard.is_pressed('esc'):
break
elif keyboard.is_pressed('space'):
bobsDecision = random.randint(0,1)
if bobsDecision == 1:
print ('bob ate')
food = 5
else:
xoy = random.randint(1,4)
if xoy == 1:
x = x + 1
elif xoy == 2:
x = x - 1
elif xoy == 3:
y = y + 1
elif xoy == 4:
y = y - 1
food = food - 1
print ('the cords are ', x, " ", y)
print ('The food supply is ', food)
I am a beginner in Python and I think I need some help with my program. Any kind of help or advice would be appreciated:)
You can see the program below, when I run it it gets stuck on the part of comparing the random ticket with the winning ticket(win_combination).
from random import choice
#Winning ticket
win_combination = input("Enter the winning combination of 4 numbers(1-digit-numbers): ")
while len(win_combination) != 4:
if len(win_combination) > 4:
win_combination = input("Reenter a shorter combination(4 one-digit-numbers): ")
elif len(win_combination) < 4:
win_combination = input("Reenter a longer combination(4 one-digit-numbers): ")
print(f"Winning combination is {win_combination}.")
#Specifying range of numbers to choose from
range = range(0, 10)
#Making a fake comparison-ticket to start of the loop
random_ticket = [0, 0]
random_ticket_string = f"{random_ticket[0]}{random_ticket[1]}{random_ticket[2]}{random_ticket[3]}"
#Params for the loop
n_tries = 0
n_guesses = 1
while random_ticket_string != win_combination:
while n_tries > 4:
random_ticket.clear()
number = choice(range)
random_ticket.append(number)
n_tries += 1
n_guesses += 1
random_ticket_string = f"{random_ticket[0]}{random_ticket[1]}"
if random_ticket_string == win_combination:
chance_to_win = f"{(1 / n_guesses) * 100}%"
print("Estimated percent to win is " + chance_to_win + ", it took " + f"{n_guesses} to match the winning combination.")
else:
n_tries = 0
I just started to learn python(self-taught) and I'm trying to do a simulation of racquetball game.
I can run the simulation with: a==15 or b==15, but when I add the extra condition: abs(a-b)>=2 IT GOES CRAZY(the simOneGame turns to an infinite loop). Here is what I tried so far:
return a == 15 or b == 15 ------> this one works perfectly
return a == 15 or b == 15 and abs(a-b) >= 2 --> this doesn't freeze but the logic is wrong(I think)
return (a == 15 or b == 15) and abs(a-b) >= 2 --> freezing
return (a == 15 or b == 15) and (abs(a-b) >= 2) --> freezing
return ((a == 15 or b == 15) and (abs(a-b) >= 2)) --> freezing
Thanks a lot and sorry if this seems too long(maybe I should've typed just the function gameOver()?...)
Here is my code so far:
from random import random
def main():
printIntro()
n, probA, probB = getInputs()
winsA, winsB = simNGames(n, probA, probB)
displayResults(winsA, winsB)
def printIntro():
print("\nVOLLEYBALL SIMULATION\n")
print("The inputs will be the number of simulations(n),")
print(" the probability player A wins his serve(probA)")
print(" and the probability player B wins his serve(probB).")
print("The program will display how many games won player A(winsA),")
print(" how many games won player B(winsB),")
print(" and the percentages of winnings for both players.\n")
def getInputs():
n = int(input("Enter the number of simulations: "))
probA = float(input("Enter the probability player A wins his serve: "))
probB = float(input("Enter the probability player B wins his serve: "))
return n, probA, probB
def simNGames(n, probA, probB):
winsA, winsB = 0, 0
for i in range(n):
# player A serves on odd games
if i % 2 == 0:
serving = "A"
# player B serves on even games
else:
serving = "B"
scoreA, scoreB = simOneGame(probA, probB, serving)
if scoreA > scoreB:
winsA = winsA + 1
else:
winsB = winsB + 1
return winsA, winsB
def simOneGame(probA, probB, serving):
scoreA, scoreB = 0, 0
while not gameOver(scoreA, scoreB):
if serving == "A":
if random() < probA:
scoreA = scoreA + 1
else:
serving = "B"
else:
if random() < probB:
scoreB = scoreB + 1
else:
serving = "A"
return scoreA, scoreB
def gameOver(a, b):
# the game is over if a player gets to 15 AND the difference is at least 2.
# this shoud work... BUT IT DOESN'T... simOneGame turns to an infinete loop with a large number of simulations...
return (a == 15 or b == 15) and abs(a-b) >= 2
def displayResults(winsA, winsB):
print("Player A won {0} games({1:1.0%})".format(winsA, winsA/(winsA+winsB)))
print("Player B won {0} games({1:0.0%})".format(winsB, winsB/(winsA+winsB)))
if __name__ == "__main__":
main()
In my programming class we are creating a dice poker game. Once a round is complete and the player receives their score, that score is supposed to be used as the new starting point for the next round. In the code you can see me trying to make "newPlayPoints" become "playingPoint", however I am unsure if that is the correct way to go about this. I'm sure the coding is a little messy due to my beginner status, but the main sections to look at would be under the function "updatePlayPoints" and the comment that reads "#Attempting to get playingPoint to become new "newPlayPoints". Thank you for the help!
from random import randint
def rollFiveDie():
allDie = []
for x in range(5):
allDie.append(randint(1,6))
return allDie
def outputUpdate(P, F):
print(P)
print(F)
def rollSelect():
rollSelected = input("Which die would you like to re-roll? (Choose 1, 2, 3, 4 and/or 5 from the list) ")
print(" ")
selectList = rollSelected.split()
return selectList
def rollPicked(toRollList, diceList):
for i in toRollList:
diceList[int(i) - 1] = randint(1,6)
def scoring(dList):
counts = [0] * 7
for value in dList:
counts[value] = counts[value] + 1
if 5 in counts:
score = "Five of a Kind", 30
elif 4 in counts:
score = "Four of a Kind", 25
elif (3 in counts) and (2 in counts):
score = "Full House", 15
elif 3 in counts:
score = "Three of a Kind", 10
elif not (2 in counts) and (counts[1] == 0 or counts[6] == 0):
score = "Straight", 20
elif counts.count(2) == 2:
score = "Two Pair", 5
else:
score = "Lose", 0
return score
def numScore(diList):
counts = [0] * 7
for v in diList:
counts[v] = counts[v] + 1
if 5 in counts:
finScore = 30
elif 4 in counts:
finScore = 25
elif (3 in counts) and (2 in counts):
finScore = 15
elif 3 in counts:
finScore = 10
elif not (2 in counts) and (counts[1] == 0 or counts[6] == 0):
finScore = 20
elif counts.count(2) == 2:
finScore = 5
else:
finScore = 0
return finScore
def printScore(fscore):
print(fscore)
print(" ")
def trackScore(score1, score2):
comScore = (score1) + (score2)
return comScore
#Starting Points
playPoints = 100
print("New round! Your points are: ", playPoints)
newPlayPoints = 100 - 10
def updatePlayPoints(nPP, PP):
nPP = PP
return nPP
def diceGame():
contPlaying = True
while contPlaying:
playing = input("It takes 10 points to play. Would you like to play the Dice Game? (Answer 'y' or 'n'): ")
print(' ')
if playing == 'y':
#First Roll
fiveDie = rollFiveDie()
outputUpdate("Your roll is...", fiveDie)
#Choosing Second Roll/Second Roll execution
pickDie = input("Which die would you like to re-roll? (Choose 1, 2, 3, 4 and/or 5 from the list) ")
print(" ")
pickDie = pickDie.split()
rollPicked(pickDie, fiveDie)
outputUpdate("Your next roll is...", fiveDie)
#Choosing Last Roll
pickDie = rollSelect()
rollPicked(pickDie, fiveDie)
outputUpdate("Your final roll is...", fiveDie)
#Scoring output
finalScore = numScore(fiveDie)
print(scoring(fiveDie))
fiveDieScore = numScore(fiveDie)
finalPoints = newPlayPoints + finalScore
print(finalPoints)
#Attempting to get playingPoint to become new "newPlayPoints"
playingPoint = trackScore(fiveDieScore, newPlayPoints)
if playingPoint > 10:
playingPoint - 10
else:
print("No more points to spend. Game Over.")
contPlaying = False
updatePlayPoints(newPlayPoints, playingPoint)
else:
contPlaying = False
def main():
diceGame()
main()
You are returning the newPlayPoints but you aren't setting them to anything. You are just calling updatePlayPoints and doing nothing with the return value. You don't need trackscore, updatePlayPoints or newPlayPoints. Instead add fiveDieScore to PlayingPoint. Also, the way it is set up now the player can start the game with no points. I am assuming that there should be an initial number of points the person starts with, I set it to 10. So now every turn you check if the player has enough points to even play and if they do immediately subtract out those points. Think about it as if it were an arcade game. You have to have a quarter to play and the first thing you do is pay to play. The way you subtract out the points does nothing. PlayingPoint - 10 is a valid line but you set it to nothing so it does nothing.
def diceGame():
contPlaying = True
PlayingPoints = 10 #Initial points
while contPlaying:
playing = input("It takes 10 points to play. Would you like to play the Dice Game? (Answer 'y' or 'n'): ")
print(' ')
if playing == 'y' and PlayingPoint >= 10:
#Subtract the points used for the turn
PlayingPoint -= 10
#First Roll
fiveDie = rollFiveDie()
outputUpdate("Your roll is...", fiveDie)
#Choosing Second Roll/Second Roll execution
pickDie = input("Which die would you like to re-roll? (Choose 1, 2, 3, 4 and/or 5 from the list) ")
print(" ")
pickDie = pickDie.split()
rollPicked(pickDie, fiveDie)
outputUpdate("Your next roll is...", fiveDie)
#Choosing Last Roll
pickDie = rollSelect()
rollPicked(pickDie, fiveDie)
outputUpdate("Your final roll is...", fiveDie)
#Scoring output
finalScore = numScore(fiveDie)
print(scoring(fiveDie))
fiveDieScore = numScore(fiveDie)
finalPoints = PlayingPoint + finalScore
print(finalPoints)
playingPoint += fiveDieScore
else:
contPlaying = False
To sum up the changes I removed TrackScore, UpdatePlayPoints, and newPlayPoints. Added the point check to the beginning rather than the end
I'm trying to simulate n games of craps. The code seems to make sense to me but I never get the right result. For example, if I put in n = 5 i.e. fives games the wins and losses sum to something greater than 5.
Here's how it's supposed to work: if initial roll is 2, 3, or 12, the player loses. If the roll is 7 or 11, the player wins. Any other initial roll causes the player to roll again. He keeps rolling until either he rolls a 7 or the value of the initial roll. If he re-rolls the initial value before rolling a 7, it's a win. Rolling a 7 first is a loss.
from random import randrange
def roll():
dice = randrange(1,7) + randrange (1,7)
return dice
def sim_games(n):
wins = losses = 0
for i in range(n):
if game():
wins = wins + 1
if not game():
losses = losses + 1
return wins, losses
#simulate one game
def game():
dice = roll()
if dice == 2 or dice == 3 or dice == 12:
return False
elif dice == 7 or dice == 11:
return True
else:
dice1 = roll()
while dice1 != 7 or dice1 != dice:
if dice1 == 7:
return False
elif dice1 == dice:
return True
else:
dice1 = roll()
def main():
n = eval(input("How many games of craps would you like to play? "))
w, l = sim_games(n)
print("wins:", w,"losses:", l)
The problem is with
if game():
wins = wins + 1
if not game():
losses = losses + 1
Instead, it should be
if game():
wins = wins + 1
else:
losses = losses + 1
In your code, you are simulating two games instead of one (by calling game() twice). This gives four possible outcomes instead of two (win/loss), giving inconsistent overall results.
In this code
for i in range(n):
if game():
wins = wins + 1
if not game():
losses = losses + 1
you call game() twice, so you play two games right there. What you want is a else block:
for i in range(n):
if game():
wins = wins + 1
else:
losses = losses + 1
Btw, you can simplify the logic with in:
def game():
dice = roll()
if dice in (2,3,12):
return False
if dice in (7,11):
return True
# keep rolling
while True:
new_roll = roll()
# re-rolled the initial value => win
if new_roll==dice:
return True
# rolled a 7 => loss
if new_roll == 7:
return False
# neither won or lost, the while loop continues ..
The code is quite literally the description you gave.
Don't do this
for i in range(n):
if game():
wins = wins + 1
if not game():
losses = losses + 1
It doesn't work out well at all.
There are numerous problems with this code. Most importantly, you're calling game() twice per loop. You need to call it once and store the result, and switch based on that.
An OO rewrite:
import random
try:
rng = xrange # Python 2.x
inp = raw_input
except NameError:
rng = range # Python 3.x
inp = input
def makeNSidedDie(n):
_ri = random.randint
return lambda: _ri(1,n)
class Craps(object):
def __init__(self):
super(Craps,self).__init__()
self.die = makeNSidedDie(6)
self.firstRes = (0, 0, self.lose, self.lose, 0, 0, 0, self.win, 0, 0, 0, self.win, self.lose)
self.reset()
def reset(self):
self.wins = 0
self.losses = 0
def win(self):
self.wins += 1
return True
def lose(self):
self.losses += 1
return False
def roll(self):
return self.die() + self.die()
def play(self):
first = self.roll()
res = self.firstRes[first]
if res:
return res()
else:
while True:
second = self.roll()
if second==7:
return self.lose()
elif second==first:
return self.win()
def times(self, n):
wins = sum(self.play() for i in rng(n))
return wins, n-wins
def main():
c = Craps()
while True:
n = int(inp("How many rounds of craps would you like to play? (0 to quit) "))
if n:
print("Won {0}, lost {1}".format(*(c.times(n))))
else:
break
print("Total: {0} wins, {1} losses".format(c.wins, c.losses))
if __name__=="__main__":
main()