【问题标题】:The nested for loop stops after 2nd iteration and asks for user input (not used input function in the python code)嵌套的 for 循环在第二次迭代后停止并要求用户输入(python 代码中未使用输入函数)
【发布时间】:2020-08-05 13:13:54
【问题描述】:

外部 for 循环没有在整个序列“L1”上进行交互。迭代在前两个值之后停止[即之后(10,3,3)]。

在显示L1中前两项的输出后,显示的消息是'程序仍在运行'并要求输入。(奇怪的是,我在整个代码中的任何地方都没有使用函数输入)

代码是-

    import random
    import array
    L1 = [(12, 8, 3), (10, 3, 3), (9, 7, 2), (6, 10, 2)]
    
    for val in L1:
        n = val[0]
        k = val[1]
        b = val[2]
        if b >= 1 and b <= (10^5) and n >= 1 and k <= (10^18) and b <= k :
            L5 = list(range(1, k+1))
            A = 0
            for z in L5:
                A = A+z
            print(A)
            while A >= n:
                L3 = []
                for w in range(0, b):
                    p = random.randint(1, k)
                    L3.append(p)
                S3 = set(L3)
                L4 = list(S3)
                if len(L4) != 3:
                    continue
                else:
                    print('I worked for', n, k, b)
                break
            else:
                print('I printed -1 for' ,n,k, b )
                

【问题讨论】:

    标签: python arrays python-3.x for-loop nested


    【解决方案1】:

    您陷入了while A &gt;= n 循环。您的A 在该循环内没有改变,if len(L4) != 3 内部的continue 阻止您到达break。因此循环变得无限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2020-01-08
      • 2021-07-28
      相关资源
      最近更新 更多