Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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 ifelse and ifelifelse. You also managed to control precisely how code is executed inside an if block using break and continue.

You got some experience with the tryexcept 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:

Or you can explore the following video courses:

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.

Download

Sample Code (.zip)

3.8 KB
Download

Course Slides (.pdf)

5.3 MB
Avatar image for Steinar Martinussen

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

Avatar image for Martin Breuss

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.

Avatar image for doink

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.