Dictonary Help

This is the place for queries that don't fit in any of the other categories.

Dictonary Help

Postby Seanliu » Sun Apr 06, 2014 3:13 pm

I don't understand - how can dictonaries help to make a Hack-Run - styled game (Like a command line)
so i am aiming to make a game (a clone of hack run)

so a playthrough would be

Code: Select all
>>login: login
>>pass: ********
>> [Checking...]
>>Command$Line$>> Startx
>> check mail
>>[Redirecting...]
>> Hack Msg....
    Hello from Mum
>> mail help
    mail (r)ead <mail>
    mail (d)elete <mail>
>>mail read Hack Msg...
>>
    hack this web from me...

--Anon



So a linux/command line styled hacking game. THX!

P.S. This is NOT a school project (my friends are struggling with variables after 3 hours)
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: Dictonary Help

Postby Mekire » Sun Apr 06, 2014 4:17 pm

Basically if I understand what you are wanting to do, you have a dictionary that has string keys (your commands) and function values.

Here is an example of what you might be trying to do (not sure):
Code: Select all
def get_help():
    return "This is an ellucidating help message"


def look_around():
    return "All you see is a computer terminal."


def jump_for_joy():
    return "It is hard to jump while sitting at a computer."


def quit_program():
    return None


def get_input():
    while True:
        user_input = input(">>> ").lower()
        if user_input in OPTION_DICT:
            return user_input
        else:
            print("Not a valid option.")


OPTION_DICT = {"help" : get_help,
               "look" : look_around,
               "jump" : jump_for_joy,
               "quit" : quit_program}

OPTIONS = ("Help", "Look", "Jump", "Quit")


def main():
    result = True
    while result:
        print("Select from {}".format(OPTIONS))
        choice = get_input()
        result = OPTION_DICT[choice]()
        if result:
            print(result, end="\n\n")


if __name__ == "__main__":
    main()

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Dictonary Help

Postby Seanliu » Sun Apr 06, 2014 9:43 pm

Yea. But I dont know how to get the input, get extra mail, ect.


--Sean
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: Dictonary Help

Postby Mekire » Sun Apr 06, 2014 11:24 pm

The effort put into your replies should match the effort of the responses you wish to recieve. I want to see you actually try to implement what you want and then ask questions about that.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Dictonary Help

Postby Seanliu » Mon Apr 07, 2014 4:46 pm

Thanks for the help. Two things I would like to point out.

1. I use Python 3.3.5, so brackets are needed.

2. (MOST IMPORTANT) it doesn't work, and I don't know why. Here is the error message:

Code: Select all
Traceback (most recent call last):
  File "/Users/Seanliu/Desktop/InfinityFighter/RoTA/HackGame.py", line 52, in <module>
    main()
  File "/Users/Seanliu/Desktop/InfinityFighter/RoTA/HackGame.py", line 43, in main
    choice = get_input()
  File "/Users/Seanliu/Desktop/InfinityFighter/RoTA/HackGame.py", line 24, in get_input
    user_input = input(">>> ").lower()
AttributeError: 'Quitter' object has no attribute 'lower'

Process finished with exit code 1

I am not sure why.

Oh yea, thanks a lot for the help!
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: Dictonary Help

Postby stranac » Mon Apr 07, 2014 4:54 pm

Show us the code you're running.
The traceback is pointless without actual code(actually, I think I might know what's going on, but can't be sure)

Edit: my guess was confirmed in IRC. Seanliu was accidentally using python 2.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1111
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests