Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Praxisbeispiel: Eine simple Wirtschaftssimulation

Heinrich-Heine-Universität Düsseldorf
# Initial State
money = 100.0
stock = 0
price = 10.0
history = []
trends = [0.2, 0.2, -0.1, -0.3]
trend_idx = 0
# Main Game Loop
while True:
    print("-" * 40)
    print(f"MARKET: Food @ {price:.2f} €")
    print("-" * 40)
    print(f"[Money: {money:.2f} € | Stock: {stock}]")
    print(f"History: {history[-3:]}")
    print("COMMANDS: 'b 1' (buy), 's 5' (sell), 'q' (quit)")
    
    cmd = input(">> ").strip().lower().split()
    
    if not cmd: 
        continue
        
    if cmd[0] == 'q': 
        print("Exiting...")
        break
    
    action = cmd[0]
    amount = int(cmd[1]) if len(cmd) > 1 else 1
    cost = amount * price
    
    if action == 'b' and money >= cost:
        money -= cost
        stock += amount
        history.append(('BUY', amount, price))
        price += 0.5 * amount  # Buy pushes price up
        print(f"\n[SUCCESS] BUY {amount} units\n")
        
    elif action == 's' and stock >= amount:
        money += cost
        stock -= amount
        history.append(('SELL', amount, price))
        price -= 0.5 * amount  # Sell pushes price down
        print(f"\n[SUCCESS] SELL {amount} units\n")
        
    else:
        print("\n[!] Invalid command or insufficient funds/stock.\n")
        continue
        
    # Advance market trend
    price = max(1.0, round(price + trends[trend_idx], 2))
    trend_idx = (trend_idx + 1) % 4
----------------------------------------
MARKET: Food @ 10.00 €
----------------------------------------
[Money: 100.00 € | Stock: 0]
History: []
COMMANDS: 'b 1' (buy), 's 5' (sell), 'q' (quit)
>>  b 1

[SUCCESS] BUY 1 units

----------------------------------------
MARKET: Food @ 10.70 €
----------------------------------------
[Money: 90.00 € | Stock: 1]
History: [('BUY', 1, 10.0)]
COMMANDS: 'b 1' (buy), 's 5' (sell), 'q' (quit)
>>  b 1

[SUCCESS] BUY 1 units

----------------------------------------
MARKET: Food @ 11.40 €
----------------------------------------
[Money: 79.30 € | Stock: 2]
History: [('BUY', 1, 10.0), ('BUY', 1, 10.7)]
COMMANDS: 'b 1' (buy), 's 5' (sell), 'q' (quit)
>>  s 2

[SUCCESS] SELL 2 units

----------------------------------------
MARKET: Food @ 10.30 €
----------------------------------------
[Money: 102.10 € | Stock: 0]
History: [('BUY', 1, 10.0), ('BUY', 1, 10.7), ('SELL', 2, 11.4)]
COMMANDS: 'b 1' (buy), 's 5' (sell), 'q' (quit)
>>  q
Exiting...