Python初学者--如何清理这段代码

Python初学者--如何清理这段代码,python,code-cleanup,Python,Code Cleanup,这是我从头构建的第一个python脚本的一部分。完整的代码设计用于在珠饰项目中使用大量珠子,并评估某些设计模式是否有效。代码是功能性的,我已经通过pylint运行了它,直到我的分数超过9.5,但是我被告知函数“number_selection”有太多分支,我正在寻求帮助清理它。我知道必须有一种简单的方法来让代码不那么冗长,并去掉所有的if语句,但我不知所措 顺便说一下,“beads”是由原始输入提供的全局变量 如有任何建议,我们将不胜感激。谢谢你的帮助 def number_suggestion

这是我从头构建的第一个python脚本的一部分。完整的代码设计用于在珠饰项目中使用大量珠子,并评估某些设计模式是否有效。代码是功能性的,我已经通过pylint运行了它,直到我的分数超过9.5,但是我被告知函数“number_selection”有太多分支,我正在寻求帮助清理它。我知道必须有一种简单的方法来让代码不那么冗长,并去掉所有的if语句,但我不知所措

顺便说一下,“beads”是由原始输入提供的全局变量

如有任何建议,我们将不胜感激。谢谢你的帮助

def number_suggestion():
''' if number entered does not work, then find the closest 
usable numbers, figure out which number provides the greatest 
number of design options and suggest that number. 
'''
next_lower = beads
next_higher = beads
list1 = []
list2 = []
add_one = 1    

# find next usable next_lower bead number
while next_lower >= 12 and next_lower % 6 != 0 and \
next_lower % 9 != 0 and next_lower % 12 != 0:
    next_lower -= 1
# find next usable next_higher bead number
while next_higher >= 12 and next_higher % 6 != 0 and \
next_higher % 9 != 0 and next_higher % 12 != 0:
    next_higher += 1

# add elements to list while counting to next usable number   
if next_lower >= 12 and next_lower % 6 == 0:
    list1.append(add_one)
if next_lower > 12 and next_lower % 9 == 0:
    list1.append(add_one)
if next_lower > 12  and next_lower % 12 == 0:
    list1.append(add_one)
if next_higher >= 12 and next_higher % 6 == 0:
    list2.append(add_one)
if next_higher > 12 and next_higher % 9 == 0:
    list2.append(add_one)
if next_higher > 12 and next_higher % 12 == 0:
    list2.append(add_one)

# make a suggestion, but not if the number works 
# in that case, return True
if beads >= 12 and beads % 6 == 0:
    return
if beads >= 12 and beads % 12 == 0:
    return
if beads >= 12 and beads % 9 == 0:
    return

# if number is less than 12, print error message 
if beads < 12:
    print('Please use 12 or more beads in your design.')
    return  

# if number doesn't work, suggest better options     
if len(list1) == len(list2):
    print('\nThere was an error.') 
    print('Try either ' + str(next_lower) + ' or ' \
    + str(next_higher) + ' beads')
elif len(list1) > len(list2):
    print('\nThere was an error.') 
    print('Try ' + str(next_lower) + ' beads instead.')
else:
    print('\nThere was an error.') 
    print('Try ' + str(next_higher) + ' beads instead.')
def number_suggestion():
''如果输入的数字无效,则查找最近的
可用数字,找出哪个数字提供了最大的
设计选项的数量和建议的数量。
'''
下一步=珠子
下一个更高=珠子
列表1=[]
列表2=[]
加一等于1
#查找下一个可用的下一个焊道编号
下一个较低>=12,下一个较低%6!=0及\
下一步\u降低%9!=0和下一个\u下限%12!=0:
下一个\u下限-=1
#查找下一个可用的下一个\u更高的胎圈编号
而下一个更高>=12和下一个更高%6!=0及\
下一个更高的%9!=0和下一个\u更高%12!=0:
下一个更高+=1
#将元素添加到列表中,同时计数到下一个可用数字
如果下一个较低值>=12,下一个较低值%6==0:
列表1.追加(添加一个)
如果下一个较低值>12且下一个较低值%9==0:
列表1.追加(添加一个)
如果next_lower>12且next_lower%12==0:
列表1.追加(添加一个)
如果下一个更高>=12且下一个更高%6==0:
列表2.追加(添加一个)
如果下一个更高>12且下一个更高%9==0:
列表2.追加(添加一个)
如果下一个更高>12且下一个更高%12==0:
列表2.追加(添加一个)
#提出一个建议,但如果这个数字有效,就不要提
#在这种情况下,返回True
如果珠子>=12且珠子%6==0:
返回
如果珠子>=12且珠子%12==0:
返回
如果珠子>=12且珠子%9==0:
返回
#如果数字小于12,则打印错误消息
如果珠子小于12:
打印('请在您的设计中使用12个或更多珠子')
返回
#如果这个数字不起作用,建议更好的选择
如果len(列表1)=len(列表2):
打印(“\n出现错误。”)
打印('尝试'+str(下一个)+'或'\
+str(上一级)+“珠子”)
elif len(列表1)>len(列表2):
打印(“\n出现错误。”)
打印('尝试'+str(下一个下一个)+'珠子')
其他:
打印(“\n出现错误。”)
打印('Try'+str(下一个更高)+'beads')

我已经检查了您的代码,并对其进行了一些更改

更改的原因在代码中以注释的形式出现

# Test a number for being >= 12 and divisible by 6
test_div_06 = lambda num: num >= 12 and num %  6 == 0

# Test a number for being >= 12 and divisible by 9
test_div_09 = lambda num: num >= 12 and num %  9 == 0

# Test a number for being >= 12 and divisible by 12
test_div_12 = lambda num: num >= 12 and num % 12 == 0

# These are tests in a list
tests = [test_div_06, test_div_09, test_div_12]

# The original if statements were repeated everywhere.
# Now they are defined as functions only once.

def passes_test(test_count):
    # Return a list of the tests applied to a number
    return [test(test_count) for test in tests]

def passes_any(test_count):
    # Return True if any of the tests are True
    return any(passes_test(test_count))

def number_suggestion(bead_count):
    '''
    if number entered does not work, then find the closest 
    usable numbers, figure out which number provides the greatest 
    number of design options and suggest that number. 
    '''
    # Sanity checks should occur first.
    # Why calculate anything if you don't need to?
    # If number is less than 12, print error message 
    if bead_count < 12:
        print("Please use 12 or more beads in your design.")
        return  
    # Number is fine; no suggestion needed
    elif passes_any(bead_count):
        return

    # Find next usable next_lower bead number
    next_lower = bead_count
    while next_lower > 12 and not passes_any(next_lower):
        next_lower -= 1
    low_count = len(passes_test(next_lower))

    # Find next usable next_higher bead number
    next_higher = bead_count
    while next_higher > 12 and not passes_any(next_higher):
        next_higher += 1
    high_count = len(passes_test(next_higher))

    # If number doesn't work, suggest better options     
    # If common behaviour occurs independent of input state,
    # it should be expressed that way.
    # That's why this print statement was moved to here,
    # because the value of counts had no effect on its occurrence.
    print("\nThere was an error.") 
    if low_count == high_count:
        print("Try either %d or %d beads" % (next_lower, next_higher))
    else:
        # The elif and else have been merged into one.
        # The reason being its that their outcome is almost identical.
        # We can calculate the next_best suggestion using an
        # inline if else statement
        next_best = next_lower if low_count > high_count else next_higher
        print("Try %d beads instead." % next_best)

if __name__ == "__main__":
    beads = raw_input("Please specify number of beads: ")
    number_suggestion(int(beads))
#测试一个数是否大于等于12并可被6整除
test\u div\u 06=lambda num:num>=12和num%6==0
#测试一个数是否大于等于12并可被9整除
test\u div\u 09=lambda num:num>=12和num%9==0
#测试一个数是否大于等于12并可被12整除
test_div_12=lambda num:num>=12和num%12==0
#这些是列表中的测试
测试=[测试分区06、测试分区09、测试分区12]
#最初的if语句到处重复。
#现在它们只被定义为函数一次。
def通过测试(测试计数):
#返回应用于数字的测试列表
返回[测试中测试的测试(测试计数)]
def通过任何测试(测试计数):
#如果任何测试为真,则返回真
返回任何(通过测试(测试计数))
def编号建议(胎圈计数):
'''
如果输入的数字无效,则查找最接近的数字
可用数字,找出哪个数字提供了最大的
设计选项的数量和建议的数量。
'''
#应首先进行健全性检查。
#如果你不需要计算,为什么还要计算呢?
#如果数字小于12,则打印错误消息
如果胎圈计数小于12:
打印(“请在您的设计中使用12个或更多珠子。”)
返回
#数字是罚款;不需要任何建议
elif通过任何(珠子计数):
返回
#查找下一个可用的下一个焊道编号
下一步=胎圈数
当next_lower>12且未通过任何(next_lower)时:
下一个\u下限-=1
低计数=长(通过测试(下一个较低))
#查找下一个可用的下一个\u更高的胎圈编号
下一个更高=珠子计数
当下一个更高>12且未通过任何(下一个更高)时:
下一个更高+=1
高计数=len(通过测试(下一个更高))
#如果这个数字不起作用,建议更好的选择
#如果常见行为独立于输入状态发生,
#应该这样表达。
#这就是为什么这份打印声明被移到这里,
#因为计数的值对其发生没有影响。
打印(“\n出现错误。”)
如果低计数==高计数:
打印(“尝试%d或%d珠子”%(下一个较低,下一个较高))
其他:
#elif和else已合并为一个。
#原因是他们的结果几乎相同。
#我们可以使用
#内联if-else语句
下一个最佳=下一个较低(如果低计数>高计数),否则下一个较高
打印(“请尝试%d个珠子。”%next\u最佳)
如果名称=“\uuuuu main\uuuuuuuu”:
珠子=原始输入(“请指定珠子数量:”)
建议数量(整数(珠子))

这个问题似乎离题了;它可能属于code-review。SetThis更适合于除了提交给codereview之外,请解释算法。谢谢——我标记了这个问题,并要求主持人移动它(根据谷歌搜索)。这是我的第一个问题,我不确定是否有更好的方法来移动这个问题,而不仅仅是在这里重新发布和删除。此外,该算法旨在建议两个数字,一个低于用户提供的数字,一个高于用户提供的数字,通过测试,从而使该数字可用于在珠饰中创建设计。基本上,