需要 Python 逻辑编程指导

发布于 2024-10-12 06:10:28 字数 655 浏览 2 评论 0原文

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

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

http://xboxflashing.com/cw2010.pdf

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

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

其次,钟摆任务令我难以置信。我对 Python 的经验非常有限,并且很难理解如何为所要求的内容设置代码。

如果您能指导我如何解决问题,那将是令人惊奇的,并且对我有巨大的帮助。我不是在寻找实际的答案 - 我可以自己在教科书中找到它的语法,我只是真的需要帮助如何开始提出解决方案 。 对任务二的任何帮助都会对我大有裨益。

谢谢您的宝贵时间。

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

I have been struggling to create 2 very small logic applications in Python. I am very new to programming and could definitely use some of your guidance, as this would possibly be very easy to someone with experience.

The first is a small program which is to calculate statistical equations based on numbers input into the program, and output results into a small table. Here is the PDF of the task:

http://xboxflashing.com/cw2010.pdf

I have managed to do all but the very last part of task 1 - everything works fine except I am not sure how to go about setting out the ranges. I had an if / elif setup, adding to a count (to count how many are in the range) - then having the count displayed under table headings, however the result displayed always came out incorrectly. Hence, I am guessing I am going about this the wrong way or overcomplicating things possibly.

Any advice on how to go about this would be greatly appreciated.

Secondly, the pendulum task is boggling my mind. I have very limited experience with Python, and am having trouble getting my head around how to set out the code for what is being asked.

If you could guide me on how to set things out, that would be amazing and a massive help to me. I am not looking for the actual answers - I can find the syntax for it myself in my textbooks, I just really need help on how to begin to set out the solution
.
Any help at all on task two would be massively beneficial to me.

Thank you for your time.

If you require any further information - please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个怎么样?

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

How about this?

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
谁把谁当真 2024-10-19 06:10:28

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

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

For the first problem, a simple implementation without any bells or whistles:

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

For the second one, it's not exactly easy - I sort of dislike pyplotlib myself for the simple reason that it can be very overwhelming at times. Sure it can do just about anything, but... :)

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文