Re: [Tutor] Help with program

2015-02-16 Thread Dave Angel

On 02/16/2015 11:27 AM, Courtney Skinner wrote:

Hello,

I am trying to build a program that approximates the value of cosine - this is 
my program so far. It is not returning the right values. Could you tell me what 
I am doing wrong?




You've got several answers that point out several problems in your code. 
 But I think you're missing a key concept.


If you're faced with a problem that's beyond your present abilities, or 
that's got you stumped, always consider factoring the problem into 
simpler ones.


To me the first thing you should factor out is a factorial function. 
Write one, that takes a positive int and returns the factorial, and test 
it against the one in the math library.


Once it's correct, then use it in the cosine problem.  Now you've got a 
simpler loop to write.  And you know part of the code works.


Next, see if you can avoid most of those three variables you're using. 
For example, What do you get when you calculate

   (-1) ** (i)

Can you use that to simplify things?


--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with program

2015-02-16 Thread Peter Otten
Courtney Skinner wrote:

> Hello,
> 
> I am trying to build a program that approximates the value of cosine -
> this is my program so far. It is not returning the right values. Could you
> tell me what I am doing wrong?
> 
> 
> def main():
> 
> import math
> 
> print("This program approximates the cosine of x by summing")
> print("the terms of this series:  x^0 / 0!, -x^2 / 2!,")
> print("x^4 / 4!, -x^6 / 6!...")
> 
> n = eval(input("How many terms should be used? "))
> x = eval(input("What value should be used for x? "))

Consider the more restrictive int() and float() instead of eval().
 
> s = 1

A nice suggestive name like "sign" instead of "s" might help you with your 
debugging efforts. Of course the same goes for your other names.

> d = 1
> e = 1
> 
> value = 0
> 
> for i in range(n - 1):
> value = value + s + (x**e / math.factorial(d))

With the name suggested above the error

 value = value + sign + (x**e / math.factorial(d))

should almost be obvious. sign plus? wait what...

Add
 print("current exponent", e)

to see another problem.

> 
> s = s * 1

That shows signs of a missing sign ;)

> e = e + 2
> d + d + 2
> 
> 
> 
> print("Approximation for cos(x) calculated by this program: ")
> print(value)
> print()
> 
> difference = math.cos(x) - value
> 
> print("Difference between this value and math.cos(x): ")
> print(difference)
> 
> main()
> 
> Thank you!
> 
> C.Skinner


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with program

2015-02-16 Thread Alan Gauld

On 16/02/15 16:27, Courtney Skinner wrote:

Hello,

I am trying to build a program that approximates the value of cosine




def main():

 import math


Its usual to do the imports outside the function at the tyop of the 
file. Python doesn't actually care much but its 'standard practice'.



 print("This program approximates the cosine of x by summing")
 print("the terms of this series:  x^0 / 0!, -x^2 / 2!,")
 print("x^4 / 4!, -x^6 / 6!...")

 n = eval(input("How many terms should be used? "))
 x = eval(input("What value should be used for x? "))


Don't use eval(). Use int() for the first one and float()
for the second. eval() is a security hazard and potentially
dangerous.


 s = 1
 d = 1
 e = 1
 value = 0

 for i in range(n - 1):
 value = value + s + (x**e / math.factorial(d))


Your description says you subtract every second value (eg -x**2/2!)
You are adding them all. Also you are adding 1(s) every time, surely
you just want to add it the first time. In other words you
want value to start at 1 and the loop to iterate from 2-(n-1)?
You also want a sign multiplier which toggles between +/-1
Also you can get Python to do most the work for you by specifying
a third step-size argument to range:

...
for e in range(2, (n*2)+1, 2): #step by 2
   value += (x**e)/math.factorial(e) * sign
   ...

You no longer need any of the other increments or variables.


 s = s * 1
 e = e + 2
 d + d + 2



 print("Approximation for cos(x) calculated by this program: ")
 print(value)
 print()


You probably want all of these outside the loop.
You might like to add a print(value) though while you are debugging.



 difference = math.cos(x) - value

 print("Difference between this value and math.cos(x): ")
 print(difference)



HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with program

2015-02-16 Thread Mark Lawrence

On 16/02/2015 16:27, Courtney Skinner wrote:

Hello,

I am trying to build a program that approximates the value of cosine - this is 
my program so far. It is not returning the right values. Could you tell me what 
I am doing wrong?


def main():

 import math


Not that it matters but imports are usually done at the top of the module.



 print("This program approximates the cosine of x by summing")
 print("the terms of this series:  x^0 / 0!, -x^2 / 2!,")
 print("x^4 / 4!, -x^6 / 6!...")

 n = eval(input("How many terms should be used? "))
 x = eval(input("What value should be used for x? "))


*DON'T* use eval, it's potentially dangerous.

n = int(input("How many terms should be used? "))
x = float(input("What value should be used for x? "))



 s = 1
 d = 1
 e = 1

 value = 0

 for i in range(n - 1):


Are you aware that this will count from zero to n - 2?


 value = value + s + (x**e / math.factorial(d))

 s = s * 1
 e = e + 2
 d + d + 2


Whoops :)


 print("Approximation for cos(x) calculated by this program: ")
 print(value)
 print()

 difference = math.cos(x) - value

 print("Difference between this value and math.cos(x): ")
 print(difference)

main()


We'd usually write:-

if __name__ == "__main__":
main()



Thank you!

C.Skinner


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with program

2015-02-16 Thread Courtney Skinner
Hello,

I am trying to build a program that approximates the value of cosine - this is 
my program so far. It is not returning the right values. Could you tell me what 
I am doing wrong?


def main():

import math

print("This program approximates the cosine of x by summing")
print("the terms of this series:  x^0 / 0!, -x^2 / 2!,")
print("x^4 / 4!, -x^6 / 6!...")

n = eval(input("How many terms should be used? ")) 
x = eval(input("What value should be used for x? "))

s = 1
d = 1
e = 1

value = 0

for i in range(n - 1):
value = value + s + (x**e / math.factorial(d))

s = s * 1
e = e + 2
d + d + 2



print("Approximation for cos(x) calculated by this program: ")
print(value)
print()

difference = math.cos(x) - value

print("Difference between this value and math.cos(x): ")
print(difference)

main()

Thank you!

C.Skinner

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with program from learning python the hard way

2012-01-19 Thread Walter Prins
Hi Jason,

On 19 January 2012 13:02, Jason Loeve  wrote:

> good day i seem to be stuck, my code is
>
> from sys import argv
>
> script, user_name = argv
>
> and i get an error ValueError: need more than 1 value to unpack
>

The implication of this message is that the value of "argv" contains only
one value, which would be true if the script was run with no command line
parameters.


>
> http://learnpythonthehardway.org/book/ex14.html
>
> i am using PyCharm 1.5.4 to run
>

I'm not familiar with PyCharm but by default IDE's obviously would not be
passing any command line parameters to programs they run.  Most IDE's have
some way of specifying the command line parameters to pass the script, but
I can't tell you how to do this in PyCharm, so if you insist on running
from PyCharm you'll have to find out how to do that on your own.

I however suggest tha you instead use PyCharm purely as an editor, and
follow the instructions in the Excercise which is to run the script from
the command prompt, with the parameters as shown in the excercise.  Then
you should have not have this problem and you should be able to follow what
the book is doing more closely.

Additionally I suggest you go back to the previous section, Excecise 13 on
"Parameters, Unpacking, Variables" since I get the sense from your question
that you may not have completed this section properly or may have glossed
over it, since if you had successfully completed this then you should not
have had the above problem in the first place.

Cheers

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with program from learning python the hard way

2012-01-19 Thread bodsda
If I say that sys.argv is a list, does that help you? 

Also, run things from a command prompt for this kind of thing, at least then 
you can guarantee what args get passed

Bodsda  
Sent from my BlackBerry® wireless device

-Original Message-
From: Jason Loeve 
Sender: tutor-bounces+bodsda=googlemail@python.org
Date: Thu, 19 Jan 2012 15:02:41 
To: 
Subject: [Tutor] help with program from learning python the hard way

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with program from learning python the hard way

2012-01-19 Thread vishwajeet singh
On Thu, Jan 19, 2012 at 6:32 PM, Jason Loeve  wrote:

> good day i seem to be stuck, my code is
>
> from sys import argv
>
> script, user_name = argv
>

Are you  passing an additional parameter while executing the script ?? you
must execute it passing an additional parameter which will get assigned to
user_name


> and i get an error ValueError: need more than 1 value to unpack
>
> http://learnpythonthehardway.org/book/ex14.html
>
> i am using PyCharm 1.5.4 to run
>
> thanks in advance
> jason
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Vishwajeet Singh
+91-9657702154 | dextrou...@gmail.com | http://bootstraptoday.com
Twitter: http://twitter.com/vishwajeets | LinkedIn:
http://www.linkedin.com/in/singhvishwajeet
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help with program from learning python the hard way

2012-01-19 Thread Jason Loeve
good day i seem to be stuck, my code is

from sys import argv

script, user_name = argv

and i get an error ValueError: need more than 1 value to unpack

http://learnpythonthehardway.org/book/ex14.html

i am using PyCharm 1.5.4 to run

thanks in advance
jason
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor