Moving from Python to D

avarisclari via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 7 20:24:20 PDT 2015


Hello,

Sorry to bother you with something trivial, but I am having 
trouble translating a block of code I wrote in Python over to D. 
Everything else I've figured out so far. Could someone help me 
understand how to get this right?

Here's the python:

scene = scenes["title"]

while 1 == 1:
     next_choice = None
     paths = scene["paths"]
     description = scene["description"]
     lines = string.split("\n")

     for line in lines:
         if len(line > 55):
             w = textwrap.TextWrapper(width=45, 
break_long_words=False)
             line = '\n'.join(w.wrap(line))
         decription += line +"\n"
     print description



     #Shows the choices
     for i in range(0, len(paths)):
         path = paths[i]
         menu_item = i + 1
         print "\t", menu_item, path["phrase"]

     print "\t(0 Quit)"

     #Get user selection

     prompt = "Make a selection ( 0 - %i): \n" % len(paths)

     while next_choice == None:
         try:
             choice = raw_input(prompt)
             menu_selection = int(choice)

             if menu_selection == 0:
                 next_choice = "quit"
             else:
                 index = menu_selection - 1
                 next_choice = paths[ index ]

         except(IndexError, ValueError):
                 print choice, "is not a valid selection"

     if next_choice == "quit":
         print "Thanks for playing."
         sys.exit()
     else:
         scene = scenes[ next_choice["go_to"] ]
         print "You decided to:", next_choice["phrase"], "\n"
         if sys.platform == 'win32':
             os.system("cls")
         else:
             os.system("clear")

I've got the very last piece set up, using consoleD to use 
clearScreen(), but The rest I'm not sure how to translate. Sorry 
for my incompetence in advance.


More information about the Digitalmars-d-learn mailing list