2

我一直在努力在 Python 中创建 2 个非常小的逻辑应用程序。我对编程很陌生,绝对可以使用您的一些指导,因为这对于有经验的人来说可能很容易。

第一个是一个小程序,根据输入到程序中的数字计算统计方程,并将结果输出到一个小表格中。这是任务的PDF:

http://xboxflashing.com/cw2010.pdf

除了任务 1 的最后一部分,我已经完成了所有工作 - 一切正常,除了我不确定如何设置范围。我有一个 if / elif 设置,增加一个计数(计算范围内有多少) - 然后在表格标题下显示计数,但是显示的结果总是不正确。因此,我猜我正在以错误的方式处理这个问题,或者可能使事情变得过于复杂。

任何有关如何解决此问题的建议将不胜感激。

其次,钟摆任务让我难以置信。我在 Python 方面的经验非常有限,并且无法弄清楚如何为所询问的内容设置代码。

如果你能指导我如何安排事情,那对我来说将是惊人的和巨大的帮助。我不是在寻找实际的答案——我自己可以在我的教科书中找到它的语法,我只是真的需要关于如何开始制定解决方案的帮助。对任务二的任何帮助都将对我大有裨益。

感谢您的时间。

如果您需要任何进一步的信息 - 请告诉我。

4

2 回答 2

1

这个怎么样?

n = int(raw_input("How many numbers?"))
nums = []
for i in range(n):
    nums[i] = float(raw_input("Enter %i th numnber >" %i))
s = sum(nums)
s2 = sum(map(lambda x:x*x,nums))
mu = s/n
mu2 = s2/n
sigma = (mu2-mu)**(0.5)
error_in_mean = sigma/(n)**(0.5)

print "x_i\tx_i-mu\t(x_i-mu)/sigma"
for x in nums:
    print "%f\t%f\t%f" %(x,x-mu,(x-mu)/sigma)

absdiff = map(lambda x:abs(x-mu),nums)
n_0 = sum(map(lambda x:(x<=sigma) , absdiff))
n_1 = sum(map(lambda x:(x>sigma)and(x<=2*sigma) , absdiff))
n_2 = sum(map(lambda x:(x>2*sigma)and(x<=3*sigma) , absdiff))
n_3 = sum(map(lambda x:(x>3*sigma) , absdiff))

print "Within 1 sigma: ",n_0
print "Between 1 and 2 sigma: ",n_1
print "Between 2 and 3 sigma: ",n_2
print "Beyond 3 sigma: ",n_3
print "Within 1 sigma: ",n_0
于 2011-01-14T20:33:36.357 回答
1

对于第一个问题,一个没有任何花里胡哨的简单实现:

numbers = []    
n = int(raw_input("Enter the amount of numbers: "))
for i in range(n):
    num = float(raw_input("Enter number %d: " % d))
    numbers.append(num)

# Calculate the statistical values here

within_1_stddev = 0
within_2_stddev = 0
within_3_stddev = 0
outside_3_stddev = 0

for num in numbers:
    if (avg - stddev) <= num < (avg + stddev):
        within_1_stddev += 1
    elif (avg - 2 * stddev) < num <= (avg - stddev) or (avg + stddev) <= num < (avg + 2 * stddev):
        within_2_stddev += 1
    elif (avg - 3 * stddev) < num <= (avg - 2 * stddev) or (avg + 2 * stddev) <= num < (avg + 3 * stddev):
        within_3_stddev += 1
    else:
        outside_3_stddev += 1

# Output here

对于第二个,这并不容易——我自己有点不喜欢 pyplotlib,原因很简单,它有时会让人不知所措。当然它可以做任何事情,但是...... :)

# Imports here

# Make a small menu here that sets the initial variables, with raw_input and
# a simple if-else structure, I guess?

timesteps = []
omegas = []
thetas = []

# Here goes the code from the PDF, but inside the loop add something like
    timesteps.append(t)
    omegas.append(omega)
    thetas.append(theta)

# Now you have the time, omega and theta in corresponding indexes at the
# timesteps, omegas and thetas variables.

# Do the plot here (consult the tutorial as suggested in the PDF)
# pyplot.show() (if I remember the name correctly) might be quite helpful here
于 2011-01-14T20:35:30.290 回答