我一直在努力学习编程,所以我一直在努力创建一个简单的程序,它将两个数字作为输入,并计算最低的公共倍数。我在Python中这样做是因为我不知道如何在Java中输入。现在发生的事情是,在我输入数字后程序就会挂起,没有任何反应。这里的任何指针将不胜感激。谢谢。
#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)
答案 0 :(得分:6)
你的逻辑有点偏。这一行:
if one % counter == 0 and two % counter == 0:
需要像这样重写:
if counter % one == 0 and counter % two == 0:
此外,您的功能应该返回 counter
而不是打印它。这有两个好处:
它会阻止脚本在末尾打印None
(函数的默认返回值)。
它允许您压缩这两行:
print counter
break
只有一个:
return counter
最后,正如@FMc在评论中指出的那样,你可以通过做两件事来提高功能的效率:
在函数的两个参数中较小的一个处开始counter
。
按此值增加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)
哦,还有一件事。 Python 2.x中的input
将其输入评估为真正的Python代码。意思是,使用不受控制的输入是危险的。
更好的方法是使用raw_input
,然后使用int
将输入显式转换为整数:
first_number = int(raw_input("Enter your first number: "))
second_number = int(raw_input("Enter your second number: "))
答案 1 :(得分:1)
试试这个:
#!/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)
答案 2 :(得分:1)
如果找不到因子,则需要让循环结束,而不是while 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)