[英]Why does this Python code not work?
Please help, I cannot figure out why this code does not work.请帮忙,我无法弄清楚为什么此代码不起作用。 I think the first loop runs forever but I don't know why!我认为第一个循环会永远运行,但我不知道为什么!
def NTN():
list1 = []
count = 0
number = 0
Start = input('Type Start Number')
while number != Start:
count = count + 1
number = number + count
Stop = input('Type Stop Number')
while number != Stop:
count = count + 1
number = number + count
if number != Stop:
(list1).append(number)
return (list1)
print(NTN())
You are increasing number
by increasing amounts in every iteration.您通过在每次迭代中增加number
来增加数量。 Here's an idea of how it is increasing.这是它如何增加的一个想法。 Assume Start
= 4假设Start
= 4
After 1 loop, count
= 1 and number
= 1, increase of 1 1次循环后, count
= 1, number
= 1,增加1
After 2 loops, count
= 2 and number
= 3, increase of 2 2 次循环后, count
= 2 和number
= 3,增加 2
After 3 loops, count
= 3 and number
= 6, increase of 3 3次循环后, count
= 3, number
= 6,增加3
Since number
is never really equal to 4, the loop never ends.由于number
永远不会真正等于4,因此循环永远不会结束。 What you need probably is while number <= Start
.您可能需要的是while number <= Start
。 That would terminate the loop after 3 iterations when number
is past 4.当number
超过 4 时,这将在 3 次迭代后终止循环。
change "number != Start" and "number != Stop" to "number < Start" and "number < Stop" in all places, and it should work.在所有地方将“number != Start”和“number != Stop”更改为“number < Start”和“number < Stop”,它应该可以工作。
What went wrong: if Start is 2, then in the first iteration of the while loop, count becomes 0+1=1 and number becomes 0+1=1;哪里出错了:如果 Start 是 2,那么在 while 循环的第一次迭代中,count 变为 0+1=1,number 变为 0+1=1; in the second iteration, count becomes 1+1=2 and number becomes 1+2=3, which bypasses 2. Since your while loop only ends when number is equal to Start, it never ends.在第二次迭代中,count 变为 1+1=2,number 变为 1+2=3,这绕过了 2。由于您的 while 循环仅在 number 等于 Start 时才结束,因此它永远不会结束。
A couple of side-points:几个侧面:
input()
returns a string; input()
返回一个字符串; if you want a number you have to convert it ie with int()
or float()
.如果你想要一个数字,你必须用int()
或float()
转换它。 (Note: if you are using Python 2.x input()
calls eval()
which is really awful design - you should be using int(raw_input())
instead.) (注意:如果你使用 Python 2.x input()
调用eval()
这真的很糟糕的设计 - 你应该使用int(raw_input())
代替。)so,所以,
# This code assumes Python 3.x
from math import ceil, sqrt
def get_int(prompt):
"""
Prompt until an integer value is entered
"""
while True:
try:
return int(input(prompt))
except ValueError:
print("Please enter an integer!")
def tri(n):
"""
Return triangular number n,
ie the sum of (1 + 2 + ... + n)
"""
# using Gaussian sum
return n * (n + 1) // 2
def reverse_tri(t):
"""
For positive integer t,
return the least positive integer n
such that t <= tri(n)
"""
# derived by quadratic formula from
# n * (n + 1) // 2 >= t
return int(ceil(((sqrt(8 * t + 1) - 1) / 2)))
def ntn(start, stop):
"""
Return a list of triangular numbers
such that start <= tri < stop
"""
a = reverse_tri(start)
b = reverse_tri(stop)
return [tri(n) for n in range(a, b)]
def main():
start = get_int('Enter a start number: ')
stop = get_int('Enter a stop number: ')
lst = ntn(start, stop + 1) # include stop number in output
print(lst)
if __name__ == "__main__":
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.