繁体   English   中英

循环输入的更好方法

[英]Better way to loop through inputs

我写了一个 python 脚本,它根据用户插入重量、拉伸长度和磅数重量(按此顺序)根据所附图表(顶行是拉伸长度,侧栏是弓磅数,以及在哪里这两个输入相交是要使用的书脊。所以基本上这个图表的“排序”过程是按插入重量(只有 2 个选项)==> 拉伸长度 ==> 弓磅数 ==> 要使用的书脊。我很难将所有内容编码为第一次项目,但我觉得有更好的方法 go 关于这个并减少代码量。下面是代码示例。可以在此处找到 Repo: https://github.com/ wayoh22/Easton-Arrow-Spine-Calculator/blob/main/Arrow_Spine_Calculator.py

箭头脊柱图表 任何帮助表示赞赏。

raw_draw_length = input("Enter Draw Length (Round to Nearest Quarter Inch): ")
raw_bow_poundage = input("Enter Bow Poundage: ")

grain_insert_list = int(50) or int(75)
draw_length_list = [23.0, 23.25, 23.5, 23.75, 24.0, 24.25, 24.25, 24.5, 24.75, 25.0, 25.25, 25.5, 25.75, 26.0, 26.25, 26.5, 26.75, 27.0, 27.25, 27.5, 27.75, 28.0, 28.25, 28.5, 28.75, 29.0, 29.25, 29.5, 29.75, 30.0, 30.25, 30.5, 30.75, 31.0, 31.25, 31.5, 31.75, 32.0]
bow_poundage_list = range(23,86)

arrow_choice = "raw_arrow_input"
grain_insert = int(raw_grain_input)
draw_length = float(raw_draw_length)
bow_poundage = int(raw_bow_poundage)

if grain_insert != int(50) and grain_insert != int(75):
    print ("Grainage not Supported")

if grain_insert == int(50):
    if draw_length < float(23):
            print("Draw Length Not Supported")

    elif draw_length >= float(23.0) and draw_length <= float(23.4):
        if bow_poundage < 69:
            print("Bow Poundage not Supported")
        if bow_poundage in range(69,86):
            print("400 Spine")
        if bow_poundage  > 85:
            print("Bow Poundage not Supported")

我要关闭你包含的图像:

箭头脊柱图

请注意,表格中的每一列始终遵循相同的模式:

col = [[400], [400], [400], [340], [340,300], [300], [300,260], [260]]

然后,

poundage_breakpoints = [23, 28, 33, 38, 43, 48, 53, 58, 63, 69, 75, 81, 86]
row_idx = min((i for i, bp in enumerate(poundage_breakpoints)
               if bow_poundage >= bp),
              default=None)
if row_idx == len(poundage_breakpoints) - 1 or row_idx is None:
    print("Bow poundage not supported.")
    return

现在我们的表中有了行索引。 我们还可以轻松计算列索引:

if not (23 <= draw_length <= 32):
    print("Draw length not supported.")
    return
col_idx = int(draw_length - 23)

现在是最后一个技巧(如我们在表中看到的那样移动列):

num_cols = 32 - 23 + 1
effective_row_idx = row_idx - (num_cols - 1 - col_idx)
if not (0 <= effective_row_idx < len(col)):
    print("Bow poundage not supported.")
    return

spines = col[effective_row_idx]
print(" or ".join(str(s) for s in spines) + " spine.")

暂无
暂无

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

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