I was practicing the first examples of input, and them I skipped to the if, else and for loops.
The whole example contains two files.
First file is sayhi.py:
name = input('What is your name?\n')
print('Hi, %s.' % name)
print('Welcome to Python.')
quest01 = input('Do you like it? Y/N\n')
if(quest01 == 'Y'):
print(f"Yeah, {name}!\nI like it too!")
else:
print(f"Oh, {name}.\nToo bad you don't like it!")
The second file is dragonloot.py:
import random
import sayhi
from rich import print
# Inventory and loot
stuff = {
"rope": 3,
"torch": 5,
"gold coins": 47,
"dagger": 2,
"arrow": 13,
"dragon tail": 0,
"legendary weapons": 3,
"girls": 0,
"wolf fangs": 33
}
dragonLoot = ['gold coins', 'dragon tail', 'dagger', 'ruby', 'silver ore', 'girls']
# Function to display the inventory with styles
def displayInventory(inventory):
# Title
print("[bold yellow]\nYour inventory, right now:[/bold yellow]\n")
# Header
print("[bold cyan]{:^18} {:>5}[/bold cyan]".format("Item", "Count"))
print("[blue]" + "-" * 24 + "[/blue]")
# Inventory items
total_items = 0
for item, count in inventory.items():
if item == "gold coins": # Style for gold coins
style = "bold yellow"
elif item == "ruby": # Style for ruby gems
style = "bold red"
elif item == "silver ore": # Style for silver ore
style = "bold blue"
elif count > 10: # Style based on item count
style = "green"
elif count == 0:
style = "orange"
else:
style = "white"
# Print the item and count with the style
print(f"[{style}]{item:<20} {count:>3}[/]")
total_items += count
# Footer
print("[blue]" + "-" * 24 + "[/blue]")
print(f"[bold magenta]Total number of items: {total_items}[/bold magenta]")
# Function to add random loot to the inventory
def add_item_to_inventory(inventory, item, min_qty, max_qty):
if item not in inventory:
inventory[item] = random.randint(min_qty, max_qty)
else:
inventory[item] += random.randint(min_qty, max_qty)
# Function to handle adding all loot
def addToInventory(inventory, addedItems, dragon_status):
for item in addedItems:
if item == 'dragon tail' and not dragon_status:
add_item_to_inventory(inventory, item, 1, 1)
elif item == "dragon tail" and dragon_status:
add_item_to_inventory(inventory, item, 0, 0)
elif item == 'gold coins':
add_item_to_inventory(inventory, item, 17, 100)
elif item == 'girls':
add_item_to_inventory(inventory, item, 0, 5)
else:
add_item_to_inventory(inventory, item, 1, 9)
# Function to get the dragon's survival status
def get_dragon_status():
alive = random.randint(0, 1)
if alive:
print("[bold green]\nCongratulations! You've looted the dragon without killing it![/bold green]")
else:
print("[bold red]\nYou've looted the dragon, but you managed to kill it. Shame on you![/bold red]")
return alive
# Function to handle special items like "girls"
def handle_special_items(inventory, item, alive_dragon):
if inventory.get(item, 0) == 1 and alive_dragon:
return "[bold cyan]\nCongratulations! You have a girlfriend![/bold cyan]"
elif inventory.get(item, 0) == 1 and not alive_dragon:
return ("[bold cyan]\nCongratulations! You have a girlfriend ...[/bold cyan]\n"
+ "[bold red]but, she hates you for killing her mom... [italic](yes! the dragon!)[/italic][/bold red]")
elif inventory.get(item, 0) > 1:
if alive_dragon:
return (
f"[bold yellow]\nYou have {inventory[item]} girls?!!\n"
+ "Sorry! But you'll have to manage it! You can keep only one.\n\n"
+ "Suggestion: [italic]dragon snacks...[/italic]\n\n"
+ "What a great way to thank the dragon for your loot![/bold yellow]"
)
elif sayhi.quest01 == "Y":
return (
f"[bold red]\nYou have {inventory[item]} girls! "
+ "Sorry! But you’ll have to manage this mess — you can only keep one!\n\n"
+ "Suggestion: \n[italic]Oh, for crying out loud…[/italic]\n"
+ "You actually killed the dragon, didn’t you?! Unbelievable!\n"
+ "Well, now you’ve got some serious damage control to do.\n"
+ "Find another dragon (yes, a [bold underline]new[/bold underline] one), try not to kill it this time, and\n"
+ "offer girls as a peace offering… Err, I mean, a snack.\n\n"
+ "Go redeem yourself, you [italic]terrible, terrible[/italic] dragon slayer![/bold red]"
)
else:
return (
f"[bold red]\nYou have {inventory[item]} girls! "
+ "Sorry! But you’ll have to manage this mess — you can only keep one!\n\n"
+ "Suggestion: \n[italic]Oh, for crying out loud…[/italic]\n"
+ "You actually killed the dragon, didn’t you?! Unbelievable!\n"
+ "Well, now you’ve got some serious damage control to do.\n"
+ "Find another dragon (yes, a [bold underline]new[/bold underline] one), try not to kill it this time, and\n"
+ "offer girls as a peace offering… Err, I mean, a snack.\n\n"
+ "Go redeem yourself, you [italic]terrible, terrible[/italic] dragon slayer [italic](and programmer)[/italic] - yes, I've noticed you don't like Python...[/bold red]"
)
if sayhi.quest01 == "Y":
return "[bold red]\nSorry! The girl didn't make it alive.[/bold red]"
else:
return "[bold red]\nSorry! The girl didn't make it alive.\n\n[italic]And it's probably because you don't like Python![/italic] SHAME ON YOU![/bold red]"
# Main Execution
alive_dragon = get_dragon_status()
addToInventory(stuff, dragonLoot, alive_dragon)
displayInventory(stuff)
print(handle_special_items(stuff, "girls", alive_dragon))
Output example 1:
What is your name?
Daniel
Hi, Daniel.
Welcome to Python.
Do you like it? Y/N
N
Oh, Daniel.
Too bad you don't like it!
Congratulations! You've looted the dragon without killing it!
Your inventory, right now:
Item Count
------------------------
rope 3
torch 5
gold coins 67
dagger 8
arrow 13
dragon tail 0
legendary weapons 3
girls 4
wolf fangs 33
ruby 3
silver ore 1
------------------------
Total number of items: 140
You have 4 girls?!!
Sorry! But you'll have to manage it! You can keep only one.
Suggestion: dragon snacks...
What a great way to thank the dragon for your loot!
Output example 2:
What is your name?
Daniel
Hi, Daniel.
Welcome to Python.
Do you like it? Y/N
N
Oh, Daniel.
Too bad you don't like it!
You've looted the dragon, but you managed to kill it. Shame on you!
Your inventory, right now:
Item Count
------------------------
rope 3
torch 5
gold coins 142
dagger 7
arrow 13
dragon tail 1
legendary weapons 3
girls 0
wolf fangs 33
ruby 8
silver ore 6
------------------------
Total number of items: 221
Sorry! The girl didn't make it alive.
And it's probably because you don't like Python! SHAME ON YOU!
Output example 3:
What is your name?
Daniel
Hi, Daniel.
Welcome to Python.
Do you like it? Y/N
N
Oh, Daniel.
Too bad you don't like it!
You've looted the dragon, but you managed to kill it. Shame on you!
Your inventory, right now:
Item Count
------------------------
rope 3
torch 5
gold coins 107
dagger 5
arrow 13
dragon tail 1
legendary weapons 3
girls 4
wolf fangs 33
ruby 3
silver ore 6
------------------------
Total number of items: 183
You have 4 girls! Sorry! But you’ll have to manage this mess — you can only keep one!
Suggestion:
Oh, for crying out loud…
You actually killed the dragon, didn’t you?! Unbelievable!
Well, now you’ve got some serious damage control to do.
Find another dragon (yes, a new one), try not to kill it this time, and
offer girls as a peace offering… Err, I mean, a snack.
Go redeem yourself, you terrible, terrible dragon slayer (and programmer) - yes, I've noticed you
don't like Python...