Question: Hello, I am struggling with understanding how for and while loops work. I'm working on code in class that is building on this area and

Hello, I am struggling with understanding how for and while loops work. I'm working on code in class that is building on this area and need some explanation on how to add these techniques.

I've posted one question but, now another has come up. This one involves a craps game we're running.

The request is:

  1. Write a Python program in a .py file that includes the following:
    1. First, roll two six-sided dice (use variables and the random number generator) and store the sum in a variable.
    2. If the sum is 7 or 11, print a message that the player is a winner in one roll of the dice.
    3. If the roll is a 2, 3, or 12, print a message that the player is a loser in one roll of the dice.
    4. For any other number, the player has established a "point". Continue to roll and sum the dice (print the sum for each roll) until either a 7 occurs or the point sum occurs a second time.
    5. If the roll now is a 7, print a loser message with the total number of rolls.
    6. If the point sum is rolled, print a winning message with the total number of rolls.

The code I have written so far is:

import random

startGame = input("Press enter to start the game!") if startGame == "": print("Good luck!") else: print("Invalid Entry")

dieOne = random.randint(1,6) dieTwo = random.randint(1,6) roll = dieOne + dieTwo

if roll == 7 or roll == 11: result = "winConditionOne" elif roll == 2 or roll == 3 or roll == 12: result = "loseConditionOne" else: result = "gameContinues" point = roll print(point, "is your new point. Please roll again. Good luck!")

while result == "gameContinues": dieOne = random.randint(1,6) dieTwo = random.randint(1,6) roll = dieOne + dieTwo

if roll == point: result == "winConditionTwo" elif roll == 7: result = "lostConditionTwo" else: result = "gameContinues"

if result == "winConditionOne": print("Congrats you rolled", roll,"you've won!") elif result == "loseConditionOne": print("You rolled",roll,"better luck next time!") elif result == "winConditionTwo": print("You rolled", roll,"you've won!") else: print("You rolled", roll,"better luck next time!")

I know there is an issue with my loop because even when I run the code and they do not immediately win or lose, it moves on to the second roll only to hit 7 every single time without failure. I imagine the loop is written incorrectly and not that I just happen to be very unlucky. The issue may be in my last line else: print("You rolled", roll,"better luck next time!") because that is the only outcome I am getting but once again I do not understand why. Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!