2

我一直在尝试更深入地编程,所以我一直在尝试制作一个简单的程序,它以两个数字作为输入,并计算最小公倍数。我在 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)
4

3 回答 3

6

你的逻辑有点不对劲。这一行:

if one % counter == 0 and two % counter == 0:

需要像这样重写:

if counter % one == 0 and counter % two == 0:

此外,您的函数应该返回 counter而不是打印它。这有两个优点:

  1. 它将阻止脚本None在最后打印(函数的默认返回值)。

  2. 它允许您压缩这两行:

    print counter
    break
    

    变成一个:

    return counter
    

最后,正如@FMc 在评论中指出的那样,您可以通过做两件事来提高函数的效率:

  1. counter函数的两个参数中的较小者开始。

  2. 按此值递增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)

哦,还有一件事。 input在 Python 2.x 中,将其输入评估为真正的 Python 代码。意思是,使用不受控制的输入是危险的。

更好的方法是使用raw_input然后将输入显式转换为整数int

first_number = int(raw_input("Enter your first number: "))
second_number = int(raw_input("Enter your second number: "))
于 2013-12-27T00:48:55.880 回答
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)
于 2013-12-27T00:55:43.980 回答
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)
于 2013-12-27T01:01:11.160 回答