2016-09-24 5 views
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) 

die ganze Sache scheintPython Wörterbuch Befehl zur Eingabe

+0

Was ist Ihre Frage? – edwinksl

+0

Es * ist * voller Syntaxfehler. Welche Schritte zur Fehlerbehebung haben Sie unternommen? – idjaw

+0

Ich bin noch sehr neu dabei. nachdem ich versucht habe, es ein paar Mal durch die Shell laufen zu lassen und dann die Bücher ein paar Foren zu cheaten und jetzt bin ich hier. Ich würde Ihre Hilfe sehr schätzen –

Antwort

2

@ idjaw ist voll von Syntaxfehlern zu sein Kommentar ziemlich richtig ist. Es hat mehr Fehler als Codezeilen, was mich dazu bringt zu denken, dass Sie einige der Aussagen isoliert bearbeiten müssen, bis sie Sinn ergeben, bevor Sie versuchen, sie alle zusammen zu kombinieren.

Hier ist ein Knirschen durch Syntax/Strukturfehler Runde 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, beheben diese Fehler, Runde 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 diese Dinge und Sie bekommen so etwas wie:

# 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.") 

Versuchen Sie online unter repl.it: https://repl.it/Dgeh

+0

genial, das hilft viel hilft, ich lerne nur Python und denke im Weg über meinen Kopf immer noch, aber ich langsam auf, viele dumme Fehler und immer vergessen ich muss mache das Universum, bevor ich es benutzen kann. ich danke dir sehr –