【问题标题】:How do I stop a while loop that adds numbers to a list when a certain index value is reached?当达到某个索引值时,如何停止将数字添加到列表中的 while 循环?
【发布时间】:2021-02-08 20:41:07
【问题描述】:

我创建了一个 while 循环,当其中的事物数量达到“计数”变量时,该循环需要停止,但我不太确定如何进行此操作。这是上下文的完整代码,但我遇到问题的部分在最后。此外,当用户输入错误时,代码会出现错误,这可能是我经过几次尝试后可以自己修复的问题,但我不确定如何解决。无论如何,这里的主要问题是while循环。提前感谢您的帮助。

count = int(input("How many numbers should the sequence have? "))
question = input("AP or GP? ")

# Asking the user for input on how many numbers the sequence will have
if question == "AP":
    APdiff = int(input("Insert the common difference for the AP: "))
    while type(APdiff) != int:
        print("Insert a valid input.")
        APdiff = int(input("Insert the common difference for the AP: "))
elif question == "GP":
    GPdiff = int(input("Insert the common ratio for the GP: "))
    while type(GPdiff) != int:
        print("Insert a valid input.")
        GPdiff = int(input("Insert the common ratio for the GP: "))
while question != "AP" and question != "GP":
    print("Please enter a valid input.")
    question = input("AP or GP? ")


def sequence_generator():


    #Setting up the sequences
    sequence = []
    number = 1

    #Defining the AP
    if question == "AP":
        while number in range(1, 99999999999999999999999999999999):
            sequence.append(number)
            number += APdiff
            if sequence.index() == count:
                break
    #Defining the GP
    elif question == "GP":
        while number in range(1, 99999999999999999999999999999999):
            sequence.append(number)
            number *= GPdiff
            if sequence.index() == count:
                break
    return sequence



print(sequence_generator())

【问题讨论】:

  • 这段代码根本不运行。它在 .index() 告诉你它需要一个参数时失败
  • 抱歉,我想我做了一些其他尝试,最终发送了错误版本的代码。一切都很好,人们给出了很好的答案

标签: python list loops while-loop sequence


【解决方案1】:

您可以使用while len(sequence)<count。 你最终会得到这样的东西

count = int(input("How many numbers should the sequence have? "))
question = input("AP or GP? ")

# Asking the user for input on how many numbers the sequence will have
if question == "AP":
    APdiff = int(input("Insert the common difference for the AP: "))
    while type(APdiff) != int:
        print("Insert a valid input.")
        APdiff = int(input("Insert the common difference for the AP: "))
elif question == "GP":
    GPdiff = int(input("Insert the common ratio for the GP: "))
    while type(GPdiff) != int:
        print("Insert a valid input.")
        GPdiff = int(input("Insert the common ratio for the GP: "))
while question != "AP" and question != "GP":
    print("Please enter a valid input.")
    question = input("AP or GP? ")


def sequence_generator():


    #Setting up the sequences
    sequence = []
    number = 1

    #Defining the AP
    if question == "AP":
        while len(sequence)<count:
            sequence.append(number)
            number += APdiff
    #Defining the GP
    elif question == "GP":
        while len(sequence)<count:
            sequence.append(number)
            number *= GPdiff
    return sequence



print(sequence_generator())

【讨论】:

  • 由于事先知道迭代次数,我还是更喜欢for循环。
【解决方案2】:

在满足条件语句之前,While 循环不会中断。例如:

x = 0
while (x < 5):
  print(x)
  x += 1
print('All done')

会输出

0
1
2
3
4
All done

程序会一直执行代码块,直到条件满足,然后才中断循环,不会打印5。

要在 while 循环中断时执行操作,只需将代码放在 while 块之后。

【讨论】:

  • while 循环也将按照 OP 的预期以 break 语句中断。将break 放在i += 1 之后,自己尝试一下。
猜你喜欢
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 2012-04-28
相关资源
最近更新 更多