Python Basics Exercises: Conditional Logic and Control Flow (Summary)
In this course, you practiced what you learned in Python Basics: Conditional Logic and Control Flow. You can now confidently compare values using comparison operators like <
, >
, <=
, >=
, !=
, and ==
. You’ve also accomplished a lot by building complex conditional statements using and
, or
, and not
.
You also controlled the flow of your program using if
statements. You created branches in your program using if
… else
and if
… elif
… else
. You also managed to control precisely how code is executed inside an if
block using break
and continue
.
You got some experience with the try
… except
pattern to handle errors that may occur during runtime. This is an important construct that allows your programs to handle the unexpected gracefully and keep users happy that the program didn’t crash.
For more information on the concepts covered in this course, check out the following tutorials:
- Conditional Statements in Python
- Using the “and” Boolean Operator in Python
- Using the “or” Boolean Operator in Python
- Using the “not” Boolean Operator in Python
- Python “for” Loops (Definite Iteration)
- Python “while” Loops (Indefinite Iteration)
- Python Exceptions: An Introduction
Or you can explore the following video courses:
- Conditional Statements in Python (
if
/elif
/else
) - Using the Python
and
Operator - Using the Python
or
Operator - Using the Python
not
Operator for
Loops in Python (Definite Iteration)- Mastering
while
Loops - Raising and Handling Python Exceptions
To continue your Python learning journey, check out the other Python Basics courses. You might also consider getting yourself a copy of Python Basics: A Practical Introduction to Python 3.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Congratulations for making it to the end of this Python Basics Exercises course. You’ve completed the challenge, which had quite a big description, and you tackled every single point there and even managed to fulfill also the stretch goals. Nice work on that. Let’s do a quick recap.
00:18 In this course, you practiced using Boolean comparators, logical operators, conditional logic, exception handling, looping, and control flow statements, and you’ve done that first by doing smaller review exercises and then putting it all together in a bigger project.
00:36 You’ve also encountered some tips along the way. You learned that it’s a good idea to use code comments to help you get organized, to break exercises into smaller tasks, to use descriptive variable names throughout, and to test repeatedly to see whether the code actually does what you expect it to do. If you want to dive deeper into the topic, here are some additional resources that you can check out.
00:58 On the Real Python site, we also have a course and a tutorial on conditional statements in Python. You can also check out the dedicated courses and tutorials on each of the Boolean operators,
01:12
as well as the courses and tutorials on definite iteration with for
loops and indefinite iteration with while
loops, and also on exception handling.
01:24 Again, congratulations for making it to the end, and thanks for joining me in this Real Python course.
Martin Breuss RP Team on Nov. 22, 2023
@Steinar Martinussen making it work with just conditional logic is perfectly fine! 😃👏
And thanks for the feedback that the functions and loops course from earlier in the learning path didn’t provide enough information or training to properly follow along with my refactoring here.
doink on June 1, 2024
Thanks Martin for your guidance.
This is my solution:
from random import randint
from time import sleep
player_health = 100
monster_health = 150
_round = 0
print(f"{player_health=}, {monster_health=}")
while True:
try:
user_input = input("Attack or Heal: ").lower()
if user_input == 'q':
print("Monster says: " "You are a loser!, Hahahaha")
break
elif user_input == 'a':
print("\n")
_round += 1
print(f"Round {_round}")
damage = randint(10 , 15)
if damage % 3 == 0:
monster_health = monster_health - 2*damage
print("******double damage!******")
else:
monster_health = monster_health - damage
if monster_health <= 0:
print("Monster says: " "Noooo!, i'll be back!")
print(f"{monster_health=}")
print("You Win!")
break
else:
print("Monster says: " "I don't feel anything!")
print(f"{monster_health=}")
print("\n")
print("Monster says: " "Now is my turn, Hahahahaha")
sleep(1)
damage = randint(15, 20)
if damage % 3 == 0:
player_health = player_health - 2*damage
print("******double damage!******")
else:
player_health = player_health - damage
if player_health <= 0:
print("Monstaer says: " "You lose!, hahahaha")
break
else:
print(f"{player_health=}")
print("\n")
elif user_input == 'h':
player_health += 30
if player_health > 100:
player_health = 100
print(f"{player_health=}")
print("\n")
else:
print("Press 'a', 'h' or 'q'")
except KeyboardInterrupt:
print("Monster says: " "You will die slow, slow!")
Become a Member to join the conversation.
Steinar Martinussen on Nov. 19, 2023
I made this work with just conditional logic and flow. As the instructor was defining his own functions to not repeat himself, I felt I was missing some knowledge around those. Maybe we seen basic stuff of it so far, but not how to pass values, how the return statement work etc. So I made the whole thing work great, but still felt that I could’ve done better if there were more around defining own functions before in the learning path. However, I will find some info about this and dig into it myself since I’m now intrigued