Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

Python Game - Dragon Cave Game

 The Dragon Cave Game 🐲

Python Game - Dragon Cave Game
Python Game - The Dragon Cave

This's a simple fun game for python beginners in coding. A very short game with a short introduction that goes like this:

You are in a land full of dragons. In front of you,

you see two caves. In one cave, the dragon is friendly,

and will share his treasure with you. The other dragon

is greedy and hungry, and will eat you on sight.

Which cave will you go into? (1 or 2 ?)

The trick here is that the result (consequences) of your choice may vary depending on your luck (a random result). The funny part is, you can choose the the same number twice, but you get different results.

And here's the code of the game:

import random
import time

def displayIntro():
    print("You are in a land full of dragons. In front of you,")
    print("you see two caves. In one cave, the dragon is friendly,")
    print("and will share his treasure with you. The other dragon")
    print("is greedy and hungry, and will eat you on sight.")
    print()

def chooseCave():
    cave = ""
    while cave != "1" and cave != "2":
        print("Which cave will you go into? (1 or 2 ?)")
        cave = input()

    return cave

def checkCave(chosenCave):
    print("You approach the cave ... ")
    time.sleep(2)
    print("It is dark and spooky ... ")
    time.sleep(2)
    print("A larg dragon jumps out in front of you! He opens his jaws and ... ")
    print()
    time.sleep(2)

    friendlyCave = random.randint(1 , 2)
    if chosenCave == str(friendlyCave):
        print("Give you his treasure!")
    else:
        print("Gobbles you down in one bite!")

playAgian = "yes"

while playAgian == "yes" or playAgian == "y":
    displayIntro()
    caveNumber = chooseCave()
    checkCave(caveNumber)
    print("Do you want to play again? (yes or no)" )
    playAgian = input()


This game has been coded by Al Sweigart, author of "Automate the Boring Stuff with Python"

Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst