Question

In: Computer Science

Write this code in python Debugging: Use the Debugging Coin Toss code below as the basis...

Write this code in python Debugging:

Use the Debugging Coin Toss code below as the basis for this project. Get the program running exactly as it appears in the text. It will run as written. Although it runs, does it run (behave) correctly? That is where you will focus your debugging efforts.

Code:

import random

guess = ' '

while guess not in ('heads', 'tails'):

     print('Guess the coin toss! Enter heads or tails: ')

     guess = input()

toss = random.randint(0, 1) # 0 is tails, 1 is heads

if toss == guess:

    print('You got it!')

else:

    print('Nope! Guess again!')

    guess = input()

    if toss == guess:

        print('You got it!')

   else:

        print('Nope. You are really bad at this game.')

Tasks Your program is to accomplish the following:

1. Welcome the user to the program

2. Keep the program flow of the original text. You will probably add to the original text, but totally rewriting the code is neither required nor desired.

3. Use debugging tools to identify if/where there is an issue with the program

4. Required: Enable logging and place logging messages in your source code. See text examples for purpose and placement of logging messages.

At a minimum, have start and end program logging messages and logging messages when variable values are changed/updated.

Write the log messages to a file ‘coin_toss_log.txt’. Log messages should not go to the screen.

5. Required: Use assertions in your source code. At a minimum, place two assertions in your code.

I recommend placing assertions to do ‘sanity’ checks on variable (guess and toss) values and types.

6. Required: Leave all your debugging code in your source code. If I don’t see it, you didn’t do it.  

7. Recommended. Run your coin_toss program with IDLE’s debugger enabled. Watch the variables closely; value and type.

8. Optional. Use other tools addressed in our text. Experiment, explore, play.  

Notes - Study the code and determine what debugging tools you can use, and where to use them, to help you determine if this program is running correctly, and if not, how to correct it. - Work on your screen output. Put effort towards an attractive output that a user/gamer would find appealing. -

Solutions

Expert Solution

****This requires some effort so please drop a like if you are satisfied with the solution****

I have satisfied all the requirements of the question, I have debugged the code, corrected the functionality and adding logging messages to be written to a file and I'm providing screenshots of code, output and output file for your reference...

Code:

import random
import logging

logging.basicConfig(filename="C://Users/THE__INFINITY/Desktop/coin_toss_log.txt",
                    format='%(asctime)s %(message)s',
                    filemode='w')
log = logging.getLogger()
log.setLevel(logging.DEBUG)
guess = ' '
print("Welcome to the coin toss Game!\n")
while guess not in ('heads', 'tails'):
    print('Guess the coin toss! Enter heads or tails: ')
    guess = input()
log.debug("guess value changed to " + guess)
assert guess == "heads" or guess == "tails"
toss = random.randint(0, 1)  # 0 is tails, 1 is heads
log.debug("toss value changed to " + str(toss))
# this is the debug code added to achieve necessary functionality
if toss == 0:
    toss = "tails"
else:
    toss = "heads"
log.debug("toss value changed to " + toss)
assert toss == "heads" or toss == "tails"
if toss == guess:
    log.debug("user won in 1st chance")
    print('You got it!')
else:
    print('Nope! Guess again!')
    guess = input()
    if toss == guess:
        print('You got it!')
        log.debug("user won in 2nd chance")
    else:
        print('Nope. You are really bad at this game.')
        log.debug("user lost")

Output Screenshot:

File output : coin_toss_log.txt :

Code Screenshot:


Related Solutions

USE R TO WRITE THE CODES! # 2. More Coin Tosses Experiment: A coin toss has...
USE R TO WRITE THE CODES! # 2. More Coin Tosses Experiment: A coin toss has outcomes {H, T}, with P(H) = .6. We do independent tosses of the coin until we get a head. Recall that we computed the sample space for this experiment in class, it has infinite number of outcomes. Define a random variable "tosses_till_heads" that counts the number of tosses until we get a heads. ```{r} ``` Use the replicate function, to run 100000 simulations of...
USE R-studio TO WRITE THE CODES! # 2. More Coin Tosses Experiment: A coin toss has...
USE R-studio TO WRITE THE CODES! # 2. More Coin Tosses Experiment: A coin toss has outcomes {H, T}, with P(H) = .6. We do independent tosses of the coin until we get a head. Recall that we computed the sample space for this experiment in class, it has infinite number of outcomes. Define a random variable "tosses_till_heads" that counts the number of tosses until we get a heads. ```{r} ``` Use the replicate function, to run 100000 simulations of...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Write an application that simulates coin tossing. Let the program toss a coin each time the...
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from...
2. Write python code for the below instructions (don’t forget to use the keyword “self” where...
2. Write python code for the below instructions (don’t forget to use the keyword “self” where appropriate in your code): A. Define a Parent Class called Person a. Inside your Person class, define new member variables called name, age, and gender.   Initialize these variables accordingly. b. Define the constructor to take the following inputs: name, age, and gender.   Assign these inputs to member variables of the same name. B. Define Child Class called Employee that inherits from the Person Class...
You are given the four payout option show below for a coin toss. If you are...
You are given the four payout option show below for a coin toss. If you are using a maximin strategy, which option do you choose? Heads you win $500, tails you lose $100 Heads you win $1,000, tails you lose $110 Heads you win $50, tails you lose $25 Heads you win $5, tails you lose $0
Premise: Every coin toss thus far has come up tails. Conclusion: The next coin toss will...
Premise: Every coin toss thus far has come up tails. Conclusion: The next coin toss will come up heads Argument A proceeds from the (general to the general) / or (general to particular) /or (particular to particular) / or (particular to general) ? According to the modern view of deduction and induction, Argument A is (inductive or deductive)? NOTE: Please provide the appropriate answer and not an uncertain response.
Use python programming to write this code and provide a screen short for the code. 2....
Use python programming to write this code and provide a screen short for the code. 2. Write a function that takes one argument (a string) and returns a string consisting of the single character from that string with the largest value. Your function should contain a for loop. You can assume that the input to your function will always be a valid string consisting of at least one character. You can assume that the string will consist only of lower-case...
6. Create a probability distribution for a coin flipping game. That is, toss a coin at...
6. Create a probability distribution for a coin flipping game. That is, toss a coin at least 25 times and keep up with the number of heads and the number of tails. (8 points for each part) a. Compile your data into a probability distribution. Be sure to show that your distribution meets the properties for a probability distribution. RESULTS Trial 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19...
Write a program to toss a coin multiple times. Ask the user for how many times...
Write a program to toss a coin multiple times. Ask the user for how many times to toss the coin and then display the percentage of heads and tails (do not display each toss, just the totals). JAVA
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT