I'm learning Python from Brian Heinold's A Practical Introduction to Python Programming where exercise 24 in chapter 6 reads:
In calculus, the derivative of x4 is 4x3. The derivative of x5 is 5x4. The derivative of x6 is 6x5. This pattern continues. Write a program that asks the user for input like
x^3orx^25and prints the derivative. For example, if the user entersx^3, the program should print out3x^2.
I figured it out. Easy. However the trick is that should be solved without using int() since it has not been mentioned in the book so far. Could you please tell me how to do that?
Here is my solution:
original = input("Enter an x with a power: ")
part1 = original[2:]
part2 = original[0]
part3 = original[1]
part4 = str(int(original[2:])-1)
derivative = part1 + part2 + part3 + part4
print("The derivative is", derivative)
intis a very basic tool (very unlikely it hasn't appeared yet if you're in chapter 6).-1function which is a lot simpler.