Python 我的';最低公倍数';程序挂起,并且不';我不能输出答案
我一直在努力深入编程,所以我一直在尝试制作一个简单的程序,以两个数字作为输入,并计算最低公倍数。我在Python中这样做是因为我不知道如何在Java中获取输入。现在发生的是,在我输入数字后,程序就挂起了,什么也没发生。这里的任何指点都将不胜感激。多谢各位Python 我的';最低公倍数';程序挂起,并且不';我不能输出答案,python,function,if-statement,while-loop,lcm,Python,Function,If Statement,While Loop,Lcm,我一直在努力深入编程,所以我一直在尝试制作一个简单的程序,以两个数字作为输入,并计算最低公倍数。我在Python中这样做是因为我不知道如何在Java中获取输入。现在发生的是,在我输入数字后,程序就挂起了,什么也没发生。这里的任何指点都将不胜感激。多谢各位 #LCM Calculator #Author: Ethan Houston #Language: Python #Date: 2013-12-27 #Function: Program takes 2 numbers as input, an
#LCM Calculator
#Author: Ethan Houston
#Language: Python
#Date: 2013-12-27
#Function: Program takes 2 numbers as input, and finds the lowest number
# that goes into each of them
def lcmCalculator(one, two):
""" takes two numbers as input, computes a number that evenly
divides both numbers """
counter = 2 #this is the number that the program tried to divide each number by.
#it increases by 1 if it doesn't divide evenly with both numbers.
while True:
if one % counter == 0 and two % counter == 0:
print counter
break
else:
counter += 1
print "\nThis program takes two numbers and computes the LCM of them...\n"
first_number = input("Enter your first number: ")
second_number = input("Enter your second number: ")
print lcmCalculator(first_number, second_number)
你的逻辑有点错误。这一行:
if one % counter == 0 and two % counter == 0:
需要像这样重写:
if counter % one == 0 and counter % two == 0:
此外,函数应该返回计数器
,而不是打印它。这有两个好处:
None
(函数的默认返回值)print counter
break
只需一个:
return counter
计数器
计数器
以下是解决所有这些问题的脚本版本:
#LCM Calculator
#Author: Ethan Houston
#Language: Python
#Date: 2013-12-27
#Function: Program takes 2 numbers as input, and finds the lowest number
# that goes into each of them
def lcmCalculator(one, two):
""" takes two numbers as input, computes a number that evenly
divides both numbers """
counter = min_inp = min(one, two)
while True:
if counter % one == 0 and counter % two == 0:
return counter
else:
counter += min_inp
print "\nThis program takes two numbers and computes the LCM of them...\n"
first_number = input("Enter your first number: ")
second_number = input("Enter your second number: ")
print lcmCalculator(first_number, second_number)
哦,还有一件事
#!/usr/local/cpython-2.7/bin/python
def lcmCalculator(one, two):
""" takes two numbers as input, computes a number that evenly
divides both numbers """
counter = 2 #this is the number that the program tried to divide each number by.
#it increases by 1 if it doesn't divide evenly with both numbers.
while True:
if counter % one == 0 and counter % two == 0:
break
else:
counter += 1
return counter
print "\nThis program takes two numbers and computes the LCM of them...\n"
first_number = int(input("Enter your first number: "))
second_number = int(input("Enter your second number: "))
print lcmCalculator(first_number, second_number)
如果没有找到因子,则需要让循环结束,而不是
,而True:
def lcmCalculator(one, two):
counter = 2
while counter <= min(one, two):
if one % counter == 0 and two % counter == 0:
return counter
else:
counter += 1
return "No common factor found"
print "\nThis program takes two numbers and computes the LCM of them...\n"
first_number = input("Enter your first number: ")
second_number = input("Enter your second number: ")
print lcmCalculator(first_number, second_number)
def液晶计算器(一、二):
计数器=2
同时计数器也可以更快地递增——以两个输入中的较大值递增,对吗?@FMc-不,实际上不是。要亲自查看,请复制我答案中的脚本,并将递增行替换为counter+=max(一,二)
。然后,运行它并给它两个数字,例如2
和3
。它无限循环。对,但min(一,二)
是正确的。:)如果我们在寻找倍数,我们应该能够跨过一倍或另一倍,前提是我们在正确的位置启动计数器——我猜也是min(1,2)
。在这种情况下,你可以轻松地从max(1,2)
开始,因为最小倍数必须大于或等于两个数字中的最大值。@Zakum我想我同意你的看法。。。但这是不正确的这个问题是一个错误陷阱。您必须在分钟(一,二)
开始,与您的步幅相同。否则,您可能会永远循环,因为您正处于一个无望的循环中:例如,尝试13和17。如果您按照传统的理解计算LCM,那么您的两个代码注释都是反向的:例如,“查找每个注释中的最小值”使其听起来像是在寻找1
和2
的因素。但是你在寻找它们的倍数,对吗?在Python2中,你应该避免使用input
函数(它可以从用户输入中执行任意Python代码!)——相反,当你想要整数时,使用int(原始输入(“输入你的第一个数字”)
。如果升级到Python3,则input
的行为类似于旧的raw_input
(旧的input
行为正是因为这个问题而消失了),它变成了int(input(…)
)。另外,在风格方面,考虑在ItToals.CONTUTE()中使用计数器<代码>:,而不是手动递增计数器。在代码中使用while(True)
语句通常不是一个好主意,因为它可能会运行“while True”,比如无限。因此,我建议使用while(counter@Zakum没有问题,而True
。这甚至是一个非常常见的模式。他正在寻找最小的公共倍数()所以计数器必然比这两个数字都大。虽然找到上限通常是个好主意。而且你的解决方案应该在函数lcmCalculator中提供一个返回语句。:)对,也许我误解了这个问题?从代码注释来看,op似乎在寻找返回语句的最低公因数。
def lcmCalculator(one, two):
counter = 2
while counter <= min(one, two):
if one % counter == 0 and two % counter == 0:
return counter
else:
counter += 1
return "No common factor found"
print "\nThis program takes two numbers and computes the LCM of them...\n"
first_number = input("Enter your first number: ")
second_number = input("Enter your second number: ")
print lcmCalculator(first_number, second_number)