2

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.

Daren P
  • 21
  • 2

2 Answers2

1

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
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
  • Hi, Thank you for your efforts, however this syntax seems far more complex than the work that I have studied. I believe it needs to be using the simplest python code that you can get, like with my attempt at Task1 shown above. Apologies I should have added that code sooner. – Daren P Jan 14 '11 at 20:54
  • I didn't actually downvote you, it must have been someone else. I couldnt figure out why its not letting me vote (im logged in but says i need to log in to vote). – Daren P Jan 16 '11 at 13:12
  • Oh yes, I didn't think you downvoted me, Daren P. I agree that the syntax isn't the simplest. Though if you continue learning Python, I'd strongly recommend going over map, filter, and lambda. – highBandWidth Jan 17 '11 at 00:05
1

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
Shadikka
  • 4,116
  • 2
  • 16
  • 19
  • Hi, your code for Task1 looks very promising. I will try to implement this when I am next in the lab tomorrow morning. Unfortunately I do not have the means to run/test the program at home - but it does look like EXACTLY what i am looking for! As for the code for Task2, that also helps me understand a bit and I will try and create some code for that tomorrow in the lab, and get back to you if i am unable to come up with anything. Thank you so much for your time – Daren P Jan 14 '11 at 20:56
  • @Daren P FYI, you can write and run Python code using your browser: http://codepad.org/ :) – Bolo Jan 14 '11 at 22:10
  • Wow, didn't know that - this will definitely come handy... thank you. – Daren P Jan 15 '11 at 01:06
  • I would like to say that I have been able to complete section 1 now because of your help, and thanks! I'm really stuck on task 2 still, I have done the imports, defined the variables, added the raw_inputs, copied the code across from the PDF. Now, do I just create more loops with different dt variable values to produce the half and double timesteps? Or is that something to do with the "append" syntax you have given me (i havent studied that yet, and am not sure how to use that). As for the pyplots, ill get onto that after I figure out the base of it all – Daren P Jan 16 '11 at 19:38
  • list.append() simply adds an item to the list, that's nothing fancier. :) With the raw_input menu system, you could just change the constants the loop given in the PDF requires _if_ that is enough to do the calculations given in the assignment. Not sure if it is, I don't feel like thinking physics right now >.>' – Shadikka Jan 16 '11 at 19:45
  • I have edited my first post with my current progress for Task2. I really have no idea how to move on from here, it just doesnt seem to be working for me at all. Any ideas? EDIT: I have really screwed up trying to get the post to display in code mode, so if anyone knows how to do that and can edit that for more that would be wonderful. – Daren P Jan 18 '11 at 23:14