海鸥别叫了476 2023-03-29 21:19 采纳率: 42.9%
浏览 22

杨辉三角,我不知道哪里出问题了,没有报错,如何解决?(语言-python)

我写的是杨辉三角,没有报错,但是没有接着运行了,不知道哪里出了问题

sh = int(input())
lo = []
list_st = []
flag = True
while True:
    if len(list_st) == 0:
        list_st.append(1)
    else:
        for i in range(len(list_st)):
            if list_st[i] == sh:  # 遍历列表中的元素,找到符合的就输出是第几个,并跳出循环
                print(i + 1)
                flag = False
                break

        if not flag:
            break  # 跳出多个循环
    # 构建杨辉三角
    list_st.append(1)  # 定义了两个列表,list_st是用来存放杨辉三角的地方
    lo.append(1)  # 列表lo是用来存放list_st杨辉三角最新一行之前的数
    if len(list_st) == 2:
        list_st.append(1)
        lo.append(1)
    else:
        for k in range(len(lo)-1, len(list_st) - 2):#生成杨辉三角里面的数字
            list_st.append(list_st[k] + list_st[k + 1])
            lo.append(list_st[k] + list_st[k + 1])
        list_st.append(1)
        lo.append(1)
  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 创建了问题 3月29日