由于我是新手,所以我开始使用python做运动。我想制作一个rever LMC计算器(最小公倍数),但是由于某种原因,像循环打印这样的简单操作对我来说似乎不起作用。我会在20分钟内停留在这个怪异的问题上,希望能对您有所帮助。这是代码:
import random
import sys
def print_list():
count_4_print = 0
while count_4_print < len(values):
print(values[count_4_print])
count_4_print += 1
def lcm(x, y):
if x > y:
greater = x
else:
greater = y
while True:
if (greater % x == 0) and (greater % y == 0):
lcm1 = greater
break
greater += 1
return lcm1
def guess(index, first_guess, second_guess):
num = 1
while lcm(first_guess, second_guess) != values[num - 1]:
first_guess = random.randrange(1, 1000000)
second_guess = random.randrange(1, 1000000)
num += 1
num = 1
if lcm(first_guess, second_guess) == values[num - 1]:
return first_guess, second_guess
num += 1
lineN = int(input())
values = []
count_4_add = 0
count_4_guess = 0
for x in range(lineN):
values.append(int(input()))
count_4_add += 1
if count_4_add >= lineN:
break
print_list()
for x in range(lineN + 1):
first, second = guess(count_4_guess, 1, 1)
count_4_guess += 1
print(first + second)
# this ^^^ doesn't work for some reason
第57行处于count_4_guess的while循环中。在此文本上方,它显示print(first_guess + second_guess)
编辑:该代码应该接受一个int x,然后提示输入x值。输出是没有x和LMC(output1,output2)的输入,其中“ LMC”是值之一。对每个值执行x次。它实际上所做的只是第一部分。它接受x并提示输入x输出,然后打印它们,但不处理数据(或者只是不打印数据)
答案 0 :(得分:1)
注意:从查看您的评论和编辑内容来看,您似乎缺少一些基本知识和/或对事物的理解。我强烈鼓励您在尝试创建像这样的整个程序之前先学习更多的编程,计算机科学和python 。
由于许多方面尚不清楚,因此很难正确回答您的问题,因此我将更新答案以反映您帖子中的任何相关更改。
现在,进入我的答案。首先,我将介绍您的一些代码,并尝试就可以改进的内容提供反馈。然后,我将介绍两种在python中计算least common multiple(LCM)的方法。
代码:
def print_list():
count_4_print = 0
while count_4_print < len(values):
print(values[count_4_print])
count_4_print += 1
注释:
print(*my_list, sep='\n')
来做到这一点。while
循环不是您应该遍历列表元素的方式。而是使用for
循环:for element in (my_list):
。代码:
def lcm(x, y):
if x > y:
greater = x
else:
greater = y
while True:
if (greater % x == 0) and (greater % y == 0):
lcm1 = greater
break
greater += 1
return lcm1
注释:
a
和b
的比较可以用greater = max(x, y)
代替。代码:
def guess(index, first_guess, second_guess):
num = 1
while lcm(first_guess, second_guess) != values[num - 1]:
first_guess = random.randrange(1, 1000000)
second_guess = random.randrange(1, 1000000)
num += 1
num = 1
if lcm(first_guess, second_guess) == values[num - 1]:
return first_guess, second_guess
num += 1
注释:
num += 1
紧接return first_guess, second_guess
之后,这意味着它永远不会执行。这些错误以某种方式相互抵消,因为据我所知,如果被执行,它将无能为力。if lcm(first_guess, second_guess) == values[num - 1]:
是完全多余的,因为上面的while
循环会检查完全相同的条件。代码:
lineN = int(input())
values = []
count_4_add = 0
count_4_guess = 0
for x in range(lineN):
values.append(int(input()))
count_4_add += 1
if count_4_add >= lineN:
break
print_list()
注释:
print_list()
不应成为事物。lineN
应该更改为line_n
,甚至更好,例如num_in_vals
。count_4_add
在lineN
循环结束时将始终等于for
。if count_4_add >= lineN
没有用。count_4_add
和count_4_guess
完全不必要,并且对该程序有害。for
循环在变量x
中产生从未使用过的值。您可以将未使用的变量替换为_
:for _ in range(10):
。in_vals = [int(input(f'Enter value number {i}: ')) for i in range(1, num_in_vals+1)]
之类的东西。同样,这取决于您实际上要做什么。根据之前引用的Wikipedia文章,计算LCM的最佳方法是使用最大公分母。
import math
def lcm(a: int, b: int) -> int:
if a == b:
res = a
else:
res = abs(a * b) // math.gcd(a, b)
return res
第二种方法是一种可能的蛮力解决方案,类似于您当前正在使用的解决方案。
def lcm(a, b):
if a == b:
res = a
else:
max_mult = a * b
res = max_mult
great = max(a, b)
small = min(a, b)
for i in range(great, max_mult, great):
if i % small == 0:
res = i
break
return res
此最终方法适用于任意数量的输入。
import math
import functools
def lcm_simp(a: int, b: int) -> int:
if a == b:
res = a
else:
res = abs(a * b) // math.gcd(a, b)
return res
def lcm(*args: int) -> int:
return functools.reduce(lcm_simp, args)
Oof,结果比我预期的更长。无论如何,请让我知道是否有任何不清楚的地方,是否犯了错误或还有其他疑问! :)