Python Forum
How do I shorten my input options? - text adventure
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I shorten my input options? - text adventure
#1
As the tittle states I want to shorten a piece of my code into something like a dictonary, im fairly new to python and dont really know how to go about this so any help or an example would be great.
	print (" Go to the -")
	print ("1.) Tavern")
	print ("2.) Arena")
	print ("3.) Quest Board")
	print ("4.) Potion Shop")
	print ("5.) Blacksmith")

	Option = input("-->")
	if (Option == "1"):
		print("You enter the Tavern")
		Tavern()
	elif (Option == "2"):
		print("You enter Arena")
		preArena()
	elif (Option == "3"):
		print("You walk to the Quest Board")
		Quest_Board()
	elif (Option == "4"):
		print("You enter the Potion Shop")
		Potion_Shop()
	elif (Option == "5"):
		print("You enter the Blacksmith")
		Blacksmith()
Reply
#2
class smart_dict(dict):
    def __missing__(self, key):
        return key

def Tavern():
    print('Typed 1')

def preArena():
    print('Typed 2')

def Quest_Board():
    print('Typed 3')

def Potion_Shop():
    print('Typed 4')

def Blacksmith():
    print('Typed 5')

options = {
    '1': ['You enter the Tavern', Tavern, 'Go to the -Tavern'],
    '2': ['You enter Arena', preArena, 'Go to the - Arena'],
    '3': ['You walk to the Quest Board', Quest_Board, 'Go to the - Quest Board'],
    '4': ['You enter the Potion Shop', Potion_Shop, 'Go to the - Potion Shop'],
    '5': ['You enter the Blacksmith', Blacksmith, 'Go to the - Blacksmith']
}

smart_dict(options)

while True:
    n=1
    print()
    for key, value in options.items():
        print('{}.) {}'.format(n, value[2]))
        n += 1

    Option = input("-->")
    if Option == options[Option]:
        continue
    print(options[Option][0])
    options[Option][1]()
I added stubs for the various functions you need to write
Reply
#3
Not actually shorter in this case, but hopefully you can gain some insight into how you could do things like this:
entrance = {"standard" : "You enter the {}.", "walk" : "You walk to the {}."}


options = [(Tavern, "Tavern", "standard"), 
           (preArena, "Arena", "standard"),
           (Quest_Board, "Quest Board", "walk"),
           (Potion_Shop, "Potion Shop", "standard"),
           (Blacksmith, "Blacksmith", "standard")]
    

print(" Go to the -")
for i, option in enumerate(options, start=1):
    print("{}.) {}".format(i, option[1]))


option = int(input("-->")) - 1
call, name, message = options[option]
print(entrance[message].format(name))
call() 
Assuming that those areas are classes you could roll the information in entrance and options into them as well.
Reply
#4
There's a tutorial on exactly that.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  python multiple try except block in my code -- can we shorten code mg24 10 5,888 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Performance options for sys.stdout.writelines dgrunwal 11 2,993 Aug-23-2022, 10:32 PM
Last Post: Pedroski55
  Want to shorten the python code shantanu97 3 1,228 Apr-25-2022, 01:12 PM
Last Post: snippsat
  loop adventure game ilikedofs 1 1,671 May-26-2021, 12:43 AM
Last Post: bowlofred
  how to make a hotkey for text adventure game myn2018 2 1,918 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Can argparse support undocumented options? pjfarley3 3 2,115 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  Choose your own adventure game noahc2004 2 2,523 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Modify Input() text catosp 6 2,825 Jun-08-2020, 10:48 PM
Last Post: deanhystad
  Selecting text as an input lazerwolf101 2 2,206 May-29-2020, 06:52 AM
Last Post: lazerwolf101

Forum Jump:

User Panel Messages

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