Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Attached is the code that I have had approved so far. If you could help me solve part 2, that would be great. 1 2
Attached is the code that I have had approved so far. If you could help me solve part 2, that would be great.
1 2 This program will prompt the user to play the game of Pig, which continues until the player gets a score of 100 or more , and then ends the game. 4 import random 5 def rollAgain() : choice str(input("Do you want to roll again? ")) . if choice != 'yes' and choice != 'Yes': 8 print("Whoops!", choice,"isn't a valid choice. Try again.") choice = str(input ("Do you want to roll again? ")) 10 if choice != 'no' and choice != 'No': 11 print ("Whoops!", choice,"isn't a valid choice. Try again.") 12 choice = str(input("Do you want to roll again? ")) 2. play one turn Let's extend our program to add a function that plays one "turn" of pig. Remember, we are solving this incrementally so there's no need to preserve your main function as you develop the solution to this next step. To play a round, add a new function called playerTurn. This function takes one parameter, the player's name (a string) and returns the number of points the player earned during their turn. The function definition will look like: def playerTurn(name): The function should follow the rules of the game as described above: 1. Initialize their turn total to 0 points 2. Roll the die and print the result. 3. If the player rolled a 1, their turn total is set to 0, print an appropriate message, and their turn ends 4. If the player rolled any other number, the turn total is incremented by the value rolled and their current turn total is displayed. 5. Ask the player if they want to roll again (using the rollAgain function) 6. If they want to roll again, start over at step 2; otherwise their turn ends 7. The player's turn total is returned from the function Note: To roll a die, use random.choice(range(1, 7)) to randomly choose a number between 1 and 6. Be sure to import random at the top of your program. Modify your main function so that it asks the player their name and then plays one turn of pig using the playerTurn function. Print out the return value of the playerTurn function. Here are some example runs: Modify your main function so that it asks the player their name and then plays one turn of pig using the playerTurn function. Print out the return value of the playerTurn function. Here are some example runs: $ python3 pig.py What is your name? Rich Rich rolled a 5 Rich earned 5 points so far this turn Do you want to roll again? yes Rich rolled a 4 Rich earned 9 points so far this turn Do you want to roll again? yes Rich rolled a 5 Rich earned 14 points so far this turn Do you want to roll again? no player Turn returned 14 $ python3 pig.py What is your name? Spencer Spencer rolled a 1 You pigged out!! player Turn returned $ python3 pig.py What is your name? Charlie Charlie rolled a 2 Charlie earned 2 points so far this turn Do you want to roll again? maybe Whoops! maybe isn't a valid choice. Try again. Do you want to roll again? ok Whoops! ok isn't a valid choice. Try again. Do you want to roll again? yes Charlie rolled a 6 Charlie earned 8 points so far this turn Do you want to roll again? yes Charlie rolled a 2 Charlie earned 10 points so far this turn Do you want to roll again? yes Charlie rolled a 3 Charlie earned 13 points so far this turn Do you want to roll again? yes Charlie rolled a 1Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started