In: Computer Science
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. -
****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: