当达到某个索引值时,如何停止将数字添加到列表的 while 循环?
How do I stop a while loop that adds numbers to a list when a certain index value is reached?
我做了一个 while 循环,当它里面的东西的数量达到 'count' 变量时需要停止,但我不太确定如何去做。这是上下文的完整代码,但我遇到麻烦的部分在最后。此外,当用户输入错误时,代码会出错,这可能是我尝试几次后可以自己修复的问题,但我不确定如何修复。无论如何,这里的主要问题是 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')
会输出
0
1
2
3
4
All done
程序会执行代码块直到条件满足,然后跳出循环,不会打印5。
要在 while 循环中断时执行操作,只需将代码放在 while 块之后。
我做了一个 while 循环,当它里面的东西的数量达到 'count' 变量时需要停止,但我不太确定如何去做。这是上下文的完整代码,但我遇到麻烦的部分在最后。此外,当用户输入错误时,代码会出错,这可能是我尝试几次后可以自己修复的问题,但我不确定如何修复。无论如何,这里的主要问题是 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')
会输出
0
1
2
3
4
All done
程序会执行代码块直到条件满足,然后跳出循环,不会打印5。
要在 while 循环中断时执行操作,只需将代码放在 while 块之后。