Discussion:
[Tutor] Abs
Job
2015-07-27 01:06:51 UTC
Permalink
I want to be able to ask a user to input an integer and print out the root and power of the given integer.

Why do you use abs(x) for this program?

I don't understand or see the link between abs() and root and powers.

This reminds me of this:
By knowing that when x%2==1 x is an odd number and when x%2 ==0 x is even, I was able to create a program that asked the user to enter 10 integers and printed out the largest odd number .

So If I understand how and why abs() is used to find the cube root of a perfect cube or how to use abs() to make the computer print out the root and power of a given integer I may make this program.

Thank you and forgive for my fuzzy thoughts.

Job

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-07-27 08:43:30 UTC
Permalink
Post by Job
I want to be able to ask a user to input an integer and print out the root and power of the given integer.
Why do you use abs(x) for this program?
You don't need to, and I'm not sure why you think you do?
I assume it says something about it in your assignment
description?

abs() converts a number to a positive so you could use
it as a guard to prevent you from taking the square root
of a negative number.
Post by Job
So If I understand how and why abs() is used to find
the cube root of a perfect cube
In Python 2 the pow() function (and ** operator) refuse
to take fractional powers of negative numbers. So even
Post by Job
pow (-8,0.33333)
you can't. You need to convert the number to positive which
is where abs comes in.

Note:
In Python v3 pow() function (and the ** operator) happily
take negative numbers. But it will tell you the root of
a negative number as a complex number result.
And abs() will convert a complex number into its magnitude
so that's a second option. if using Python 3.

In both cases you will need to manage the sign of the
final answer yourself since the use of abs() will always
convert things to a positive.

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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-07-29 08:51:26 UTC
Permalink
How is it going tutors?
You only sent it to me. Please use Reply All to include the list.
"*Write a program that asks the user to enter an integer and prints
two integers, /root /and /pwr/, such that 0 < pwr < 6 and root^pwr
(root**pwr) is equal to the integer entered by the user. If no such
pair of integers exists, it should print a message to that effect*."
I would like to solve this problem myself so please don't give me the
solution.
I need to learn how in the world do find the root and power of an
integer that x user entered? I haven been looking on the python
website for an appropriate function but I have not.
The only function you need is pow()
Or you could do it without a function by using the ** operator.

You want to try various integer values and see if the result is the
users input.
That means you need a loop. The pwr value is set between 1 and 5 in the
assignment. The maximum root value will be the user's input (since X**1 = X,
and that will always be a valid solution!)
Is there a book you guys recommend for total beginners who have no
idea of what computer science and programming is?
Being biased, I'd recommend my web site(see below). You can get an older
version in a paper book if you must, and its mostly still applicable.
There is
also a good option by Allen Downey which focuses on the CS side of things
if that's what you want.

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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-07-29 09:19:05 UTC
Permalink
Post by Alan Gauld
How is it going tutors?
You only sent it to me. Please use Reply All to include the list.
My mistake, you sent it to the list too. For some reason my mailer
didn't show the tutor header... Its in a new thread now.
--
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Chris Roy-Smith
2015-07-27 08:24:37 UTC
Permalink
Post by Job
I want to be able to ask a user to input an integer and print out the root and power of the given integer.
Why do you use abs(x) for this program?
I don't understand or see the link between abs() and root and powers.
By knowing that when x%2==1 x is an odd number and when x%2 ==0 x is even, I was able to create a program that asked the user to enter 10 integers and printed out the largest odd number .
So If I understand how and why abs() is used to find the cube root of a perfect cube or how to use abs() to make the computer print out the root and power of a given integer I may make this program.
Thank you and forgive for my fuzzy thoughts.
Job
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
You will fine that any attempt to find the root of a negative value will
result in an error.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...