简体   繁体   English

当达到某个索引值时,如何停止将数字添加到列表中的 while 循环?

[英]How do I stop a while loop that adds numbers to a list when a certain index value is reached?

I made a while loop that needs to stop when the number of things inside of it reaches the 'count' variable, but I'm not too sure on how to go about this.我做了一个while循环,当里面的东西数量达到'count'变量时需要停止,但我不太确定如何 go 关于这个。 Here is the full code for context, but the part that I'm having trouble with is at the end.这是上下文的完整代码,但我遇到问题的部分在最后。 Also, when the user gives wrong input, the code gives an error, it's probably something that I could fix by myself after a few attempts, but I'm not exactly sure how.此外,当用户输入错误时,代码会出现错误,这可能是我在尝试几次后可以自己修复的问题,但我不确定如何解决。 Anyways, the main problems here are the while loops.无论如何,这里的主要问题是while循环。 Thanks for the help in advance.我在这里先向您的帮助表示感谢。

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

You could use while len(sequence)<count .您可以使用while len(sequence)<count You would end up with something like this你最终会得到这样的东西

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

While loops will not break until the conditional statement is met.在满足条件语句之前,while 循环不会中断。 For example:例如:

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

will output将 output

0
1
2
3
4
All done

The program will execute the block of code until the condition is met, and then break the loop and will not print 5.程序会执行代码块,直到条件满足,然后中断循环,不会打印 5。

To perform an action when a while loop is broken, simply put the code after the while block.要在 while 循环中断时执行操作,只需将代码放在 while 块之后。

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

相关问题 当到达列表的某个元素时,停止while循环 - Stopping a while loop when a certain element of the list is reached 一旦达到某个值,如何停止对列表进行迭代 - How to stop iterating over a list once a certain value is reached 当使用 Autokey 按下某个键时,如何停止 while 循环? - How do I stop a while loop when a key is pressed with Autokey? 如何编写while循环以使其在2个变量获得某些值时停止? (Python) - How can I write while loop in order to make it stop when 2 variables got certain values? (Python) 如何在某个索引处中断while循环,然后运行到列表末尾? - How to break while loop at a certain index but then run until end of a list? 达到数字时循环停止 - For loop stop when number is reached 达到最大目标数后,如何停止 pygame 中的事件循环? - How do I stop the event loop in pygame, after the maximum number of targets have been reached? 如何在列表的列表中多次索引某个字符? - How do I index a certain character multiple times in a list of a list? 如何在python的列表中找到某个值的随机实例(和索引) - How do I find a random instance (and index) of a certain value in a list in python 我如何从列表的某个索引(Python)开始做“ for each”? - How do I do a “for each” , starting at a certain index of a list (Python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM