python - [Any suggestions on how to improve my input commands]-- SOLVED -


i looking suggestions improve, simplify or tidy input commands. have seen repetition not habit.

while(char.health > 0):     print("_____________________________________")     line = input("\n>").lower().split()     if len(line) < 1:         print ("{} doesn't understand command?".format(char.name))     elif line[0] == "go":         commands.movement()     elif line[0] == "quit":         commands.quit()     elif line[0] == "rest":         commands.rest()     else:         print ("{} doesn't understand command?".format(char.name)) 

i have seen people use dict :

    cmd = {'go' : commands.movement()}  

when seems run def without them being called! have figured solution out.

cmd = { "go" : (commands.movement)} 

this new commands dict. realized commands.movement() calling def.

while (char.health > 0):     print("_________________________________________")     line = input(">").lower().split()     if len(line) < 1:         print ("invalid command!")     elif line[0] in cmd:         cmd[line[0]]()     #this calls def instead     else:         print("invalid command!") 


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -