循环输入的更好方法

2024-06-09 11:01:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个python脚本,它根据用户根据所附图表(顶行是绘图长度,侧列是弓形磅数,这两个输入相交的地方是要使用的脊线)插入重量、绘图长度和磅数重量(按此顺序)来确定要使用的箭头脊线这个图表的过程是通过插入重量(只有2个选项)=>;绘制长度==>;船首手续费==>;要使用的脊椎。我将所有内容硬编码为第一次项目,但我只是觉得有更好的方法来实现这一点并减少代码量。下面是代码示例。可以在此处找到Repo:https://github.com/wayoh22/Easton-Arrow-Spine-Calculator/blob/main/Arrow_Spine_Calculator.py

Arrow Spine chart 。感谢您的帮助

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")

Tags: inputrawiffloatlengthintinsertsupported
1条回答
网友
1楼 · 发布于 2024-06-09 11:01:47

我要离开你包括的图片:

arrow spine chart

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

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.")

相关问题 更多 >