pratikshahp commited on
Commit
efe308e
·
verified ·
1 Parent(s): 658b140

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -23,14 +23,17 @@ def randomly_select_from_json(world_data):
23
 
24
  # Town level
25
  towns = kingdom_data["towns"]
26
- town_name, town_data = random.choice(list(towns.items()))
 
 
 
 
 
 
27
  town_description = town_data["description"]
28
 
29
  # Character (NPC) level
30
- npcs = town_data.get("npcs") # Use .get() to handle missing 'npcs' key
31
- if not npcs:
32
- raise ValueError(f"No NPCs found in town: {town_name}")
33
-
34
  character_name, character_data = random.choice(list(npcs.items()))
35
  character_description = character_data["description"]
36
 
 
23
 
24
  # Town level
25
  towns = kingdom_data["towns"]
26
+
27
+ # Filter towns with NPCs only
28
+ valid_towns = {name: data for name, data in towns.items() if data.get("npcs")}
29
+ if not valid_towns:
30
+ raise ValueError(f"No towns with NPCs found in kingdom: {kingdom_name}")
31
+
32
+ town_name, town_data = random.choice(list(valid_towns.items()))
33
  town_description = town_data["description"]
34
 
35
  # Character (NPC) level
36
+ npcs = town_data["npcs"]
 
 
 
37
  character_name, character_data = random.choice(list(npcs.items()))
38
  character_description = character_data["description"]
39