[英]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.