簡體   English   中英

當達到某個索引值時,如何停止將數字添加到列表中的 while 循環?

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

我做了一個while循環,當里面的東西數量達到'count'變量時需要停止,但我不太確定如何 go 關於這個。 這是上下文的完整代碼,但我遇到問題的部分在最后。 此外,當用戶輸入錯誤時,代碼會出現錯誤,這可能是我在嘗試幾次后可以自己修復的問題,但我不確定如何解決。 無論如何,這里的主要問題是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())

您可以使用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())

在滿足條件語句之前,while 循環不會中斷。 例如:

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

將 output

0
1
2
3
4
All done

程序會執行代碼塊,直到條件滿足,然后中斷循環,不會打印 5。

要在 while 循環中斷時執行操作,只需將代碼放在 while 塊之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM