Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a def from a string
#6
What about a dict of rooms, and you pick one from there?

import random

rooms = {}

def get_next_room_from(current_room):
    next_room = None
    while not next_room or next_room == current_room:
        next_room = random.choice(list(rooms.keys()))
    return next_room

def entryway():
    print("Welcome to the entryway.  Bye!")
    next_room = get_next_room_from("entryway")
    return next_room

def conservatory():
    print("This is the conservatory.  There's plants and wicker couches here.")
    next_room = get_next_room_from("conservatory")
    return next_room

rooms["entryway"] = entryway
rooms["conservatory"] = conservatory

running = True
current_room = "entryway"
while running:
    current_room = rooms[current_room]()
    running = "q" != input("Type 'q' to stop.").lower()
Reply


Messages In This Thread
Call a def from a string - by DreamingInsanity - Jun-22-2018, 06:09 PM
RE: Call a def from a string - by buran - Jun-22-2018, 06:40 PM
RE: Call a def from a string - by Larz60+ - Jun-22-2018, 06:45 PM
RE: Call a def from a string - by ljmetzger - Jun-22-2018, 06:50 PM
RE: Call a def from a string - by volcano63 - Jun-22-2018, 08:03 PM
RE: Call a def from a string - by nilamo - Jun-22-2018, 08:04 PM
RE: Call a def from a string - by DreamingInsanity - Jun-23-2018, 08:21 AM
RE: Call a def from a string - by nilamo - Jun-25-2018, 06:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to call the values at the end of a text string? Dieselkaine 2 3,082 Jul-02-2018, 08:47 PM
Last Post: Dieselkaine

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020