0
commands = { 'a': 'far' }
while(1 == 1):
    print ("{} to{}.".format(commands.key, commands.value[0])
    (input("~~~~~Press a key.~~~~~"))
    if input(key in commands.keys()
    commands.value[1]
    if not
        print("Not a valid command.")

def far():
    print (2 + 2)

the entire thing seems to be full of syntax errors

7
  • What is your question? Commented Sep 24, 2016 at 5:05
  • It is full of Syntax Errors. What troubleshooting steps have you taken? Commented Sep 24, 2016 at 5:07
  • I'm still very new at this. after trying to run it through shell a couple times and then hitting the books a few forums some chat rooms and now I'm here. I would greatly appreciate your help Commented Sep 24, 2016 at 5:10
  • I strongly advise you read your lesson plan and tutorials really carefully. Upon further inspection there just seems to be a lot of fundamental problems going on here that a simple answer will not help. Furthermore, it is really not clear what it is you are trying to achieve without providing an explanation of what problem you are trying to solve. Commented Sep 24, 2016 at 5:19
  • 1
    Add that explanation to your question. Please read how to put together a minimal reproducible example to ensure that your question is more complete. Commented Sep 24, 2016 at 5:27

1 Answer 1

2

@idjaw's comment is pretty right. It's got more errors than lines of code, which makes me think you need to work on some of the statements in isolation until they make sense, before trying to combine them all together.

Here's a crunch through of syntax / structural errors round 1:

# These two are fine
commands = { 'a': 'far' }
while(1 == 1):

    # This is broken several times, first print ( ... ) needs matching
    # open/close parentheses and you open two but only close one. 
    # It's missing a close parenthesis at the end.
    # Second, commands.key is not a valid thing. commands.keys would be
    # but it's a function so it would need to be commands.keys().
    # Third, commands.value is not valid either. commands.value() would be.
    print ("{} to{}.".format(commands.key, commands.value[0])

    # This is valid code, but why is it in parentheses?
    (input("~~~~~Press a key.~~~~~"))

    # This is broken - input ( ... ) is  missing a close parenthesis at the end
    # It's also broken because `if` statements need a colon at the end.
    # but how come you get commands.keys() right here?
    if input(key in commands.keys()

    # This is broken - after an `if` statement, code needs to be indented.
    # It's also broken because .value isn't a thing.
    # and it's broken because there is only one value in the dictionary
    # so asking for the second one will crash.
    # It's also broken because just stating the second value won't magically 
    # call a function which matches the string in the value

    commands.value[1]

    # This is broken - if not ... what?
    # and it's missing a colon at the end.
    if not
        print("Not a valid command.")

def far():
    print (2 + 2)

OK, fix those errors, round 2:

# if you put `far` definition at the end, it hasn't been defined
# yet when you try to call it, so it needs to be higher up.

def far():
    print (2 + 2)

commands = { 'a': 'far' }

# This is fine, but it would be more idiomatic as `while True:`
while(1 == 1):

    # This is still broken, it will format all keys but only one value.
    # Even if it formatted all the values it would be broken because
    # it would output one line like:
    #
    # "dict_keys(['a', 'b']) to dict_values(['far', 'near'])"
    #
    # it needs to be in a loop, outputting one of each at a time.
    print ("{} to{}.".format(commands.keys(), commands.values()[0]))

    # suspicious - request input but do nothing with the input?
    input("~~~~~Press a key.~~~~~")

    # This is broken - `key` is not defined here
    # and if it was defined it would be broken because `x in y` tests
    # if an item is in a collection, and returns True/False
    # so this prompts the user with the prompt: "False"
    # It's broken again because the result of the input, a string,
    # is not stored so `if` is only testing if it's an empty string or not
    # and you don't know what they typed in.
    if input(key in commands.keys()):

        # This is fundamentally unfixable until the design changes,
        # also needs a change of approach
        commands.values()[0]

    # Still broken - whatever you put here won't make sense since you aren't 
    #   storing the user input. Either need to do that, or use `else`
    if not something:
        print("Not a valid command.")

Correct those things and you get something like:

# define a function to work with
def far():
    print (2 + 2)

# mapping of /keyboard keys/ to function names
commands = { 'a': 'far' }

# loop forever
while True:

    # Loop over the /dictionary keys/ and /dictionary values/
    # printing each one
    for keyboard_key, command_name in commands.items():
        print("{} to {}".format(keyboard_key, command_name))

    # ask the user to type something, and store the return value
    entry = input("~~~~~Press a key.~~~~~")

    # check if what they /entered/ is in the commands dictionary
    # and get the command, or get False if it's not there
    command = commands.get(entry, False)

    # if it did get something...
    if command:

        # lookup the function of that name in the local namespace and call it.
        # this is ugly but it's the price to pay for 
        # calling a function when you only have a string 
        # representing its name.
        # Using the function as the dictionary value 
        # would be more Pythonic but you'd have to change the way
        # you prompt the user
        locals()[command]()

    # otherwise print an error
    else:
        print("Not a valid command.")

Try online at repl.it: https://repl.it/Dgeh

Sign up to request clarification or add additional context in comments.

1 Comment

awesome that helps a lot, im just learning python and guess im way over my head still, but im slowly catching on, a lot of stupid mistakes and keep forgetting i have to make the universe before i can use it. thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.