简体   繁体   English

如何将间距合并到递归中?

[英]How do I incorporate spacing into recursive?

def PrintDT(Tennis):
    print "Split on Tennis[0]"
    for a in Tennis[1]:
        print "If tennis[0] == a " 
        if isinstance(Tennis[1][a], str):
            PrintDT(tennis)
        else:       
            print "Return:>>>5*' 'Tennis[1][a]"

So the formatting that I am supposed to achieve is : 所以我应该实现的格式是:

Split on  Outlook
 If Outlook == Sunny
      Split on  Humidity
      If Humidity == High
           Return:  No
      If Humidity == Low
           Return:    Yes
 If Outlook == Overcast
      Return:    Yes
 If Outlook == Rain
      Split on  Wind
      If Wind == Strong
           Return:   No
      If Wind == Weak
           Return:    Yes

I think that I have the recursive program down, but know I need to do the correct formatting. 我认为我有递归程序,但知道我需要做正确的格式化。 PLease HELP 请帮忙

You need to take the variable names out of the string literals you are printing. 您需要从正在打印的字符串文字中取出变量名称。 Currently, you're just printing a bunch of strings that won't change, but if you remove the variable names from the " s, you should fine. It would be helpful if we knew what Tennis was, but this should at least get you closer to the right answer. 目前,你只是打印了一堆不会改变的字符串,但是如果你从" s "删除变量名,你应该没问题。如果我们知道什么是Tennis ,这将是有帮助的,但这至少应该得到你更接近正确的答案。

def PrintDT(Tennis):
    print "Split on", Tennis[0]
    for a in Tennis[1]:
        print "If", tennis[0], "==", a
        if isinstance(Tennis[1][a], str):
            PrintDT(tennis)
        else:
            print "Return:     " + Tennis[1][a]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM