Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genetic Databases

Authors: Martin J. Bishop

1st Edition

0121016250, 978-0121016258

More Books

Students also viewed these Databases questions

Question

love of humour, often as a device to lighten the occasion;

Answered: 1 week ago

Question

orderliness, patience and seeing a task through;

Answered: 1 week ago

Question

well defined status and roles (class distinctions);

Answered: 1 week ago