diff --git a/practice_projects/coin_toss.py b/practice_projects/coin_toss.py new file mode 100644 index 0000000..f1897c7 --- /dev/null +++ b/practice_projects/coin_toss.py @@ -0,0 +1,22 @@ +#!python3 +# coin_toss.py - a simple coin toss guessing game. The player gets two guesses. + +import random + +guess = '' +while guess not in ('heads', 'tails'): + print('Guess the coin toss! Enter heads or tails:') + guess = input() +toss = ('heads', 'tails')[random.randint(0, 1)] # 0 is tails, 1 is heads + +assert toss in ('heads', 'tails'), 'toss should be heads or tails.' + +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.') diff --git a/practice_projects/test/spam016.txt b/practice_projects/test/spam016.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam017.txt b/practice_projects/test/spam017.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam018.txt b/practice_projects/test/spam018.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam019.txt b/practice_projects/test/spam019.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam020.txt b/practice_projects/test/spam020.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam021.txt b/practice_projects/test/spam021.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam022.txt b/practice_projects/test/spam022.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam023.txt b/practice_projects/test/spam023.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam024.txt b/practice_projects/test/spam024.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_projects/test/spam025.txt b/practice_projects/test/spam025.txt deleted file mode 100644 index e69de29..0000000 diff --git a/practice_questions/README.md b/practice_questions/README.md index e01f0d0..fe82c8e 100644 --- a/practice_questions/README.md +++ b/practice_questions/README.md @@ -9,3 +9,4 @@ - [Chapter 7 – Pattern Matching with Regular Expressions](ch07/README.md) - [Chapter 8 – Reading and Writing Files](ch08/README.md) - [Chapter 9 – Organizing Files](ch09/README.md) +- [Chapter 10 – Debugging](ch10/README.md) \ No newline at end of file diff --git a/practice_questions/ch10/README.md b/practice_questions/ch10/README.md new file mode 100644 index 0000000..f259cbe --- /dev/null +++ b/practice_questions/ch10/README.md @@ -0,0 +1,57 @@ +# Chapter 10 – Debugging + +> Q: 1. Write an assert statement that triggers an AssertionError if the variable spam is an integer less than 10. + +`assert(spam >= 10, 'The spam should be less than 10.')` + +> Q: 2. Write an assert statement that triggers an AssertionError if the variables eggs and bacon contain strings that are the same as each other, even if their cases are different (that is, 'hello' and 'hello' are considered the same, and 'goodbye' and 'GOODbye' are also considered the same). + +`assert(eggs.lower() != bacon.lower(), 'The eggs and bacon should contain different string.')` + +> Q: 3. Write an assert statement that always triggers an AssertionError. + +`assert(False, 'Always triggers an AssertionError')` + +> Q: 4. What are the two lines that your program must have in order to be able to call logging.debug()? + +```py +import logging +logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s') +``` + +> Q: 5. What are the two lines that your program must have in order to have logging.debug() send a logging message to a file named programLog.txt? + +```py +import logging +logging.basicConfig(filename='programLog.txt', level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s') +``` + +> Q: 6. What are the five logging levels? + +DEBUG, INFO, WARNING, ERROR, CRITICAL + +> Q: 7. What line of code can you add to disable all logging messages in your program? + +`logging.disable()` + +> Q: 8. Why is using logging messages better than using print() to display the same message? + +You can disable logging message without removing the logging function calls. You can selectively disable lower-level logging messages. + +> Q: 9. What are the differences between the Step, Over, and Out buttons in the Debug Control window? + +Step: move in the function call. +Over: execute the function call without stepping into it. +Out: execute the rest of the code until it steps out of the function it currently is in. + +> Q: 10. After you click Go in the Debug Control window, when will the debugger stop? + +the end of the program or a line with a breakpoint. + +> Q: 11. What is a breakpoint? + +debugger to pause. + +> Q: 12. How do you set a breakpoint on a line of code in IDLE? + +Set breakpoint. \ No newline at end of file