This question already has answers here:
Basics of recursion in Python
(5 answers)
Closed 7 years ago.
I have this:
def main():
input1 = input('Enter an integer for the base:')
input2 = input('Enter an integer for the exponent:')
main()
This is what I fully need to do:
Create a main, and a power function. Half done.
prompt the user to enter an integer for the base of the power. Done.
prompt the user for an integer for the exponent of the power. Done.
call the power function and print its returned value. Can do.
The recursive power function, power(base,exponent), must recursively calculate the value of the power and then return it. Need help with this.
I have been trying to learn the recursive function stuff the past two days, and cannot seem to wrap my head around it. Could someone provide me a walk through of this, so I can mess around with it, so I can understand it better? Thanks!
You basically need to multiply a given number to itself until the power reaches zero:
def power(x, y):
if y == 0:
return 1
if y >= 1:
return x * power(x, y - 1)
Related
I'm training with codewars for an upcoming bootcamp and am asked to write a simple user defined function. It passes the first four tests, but fails the last two. The challenge is posed as:
Very simple, given a number, find its opposite.
Examples:
1: -1
14: -14
-34: 34
I wrote the following code
def opposite(number):
if number > 0:
return (-number)
elif number < 0:
return (+number)
else:
return 0
However, I run into problems with the last two tests
test.assert_equals(opposite(1),-1)
test.assert_equals(opposite(25.6), -25.6)
test.assert_equals(opposite(0), 0)
test.assert_equals(opposite(1425.2222), -1425.2222)
* test.assert_equals(opposite(-3.1458), 3.1458)
* test.assert_equals(opposite(-95858588225),95858588225)
I remember reading something about floats and how the variable is stored as something slightly different than what it appears on the screen, but I don't know what to do to combat this.
The arithmetic inverse of x is −x. Therefore, the problem can be solved with:
def opposite(number):
return -number
This question already has answers here:
range() for floats
(23 answers)
Closed 2 years ago.
I'm trying to print the numbers between 0.00001 and 0.1, where i tried:
for i in range(0.00001,0.1):
print(i)
But python throws an error saying it cannot be interpreted as integers. Is it possible to do something like this in python?
range only supports integers. Lets define our own
def frange(start, stop, step):
while start < stop:
yield start
start += step
for i in frange(0.5, 1.0, 0.1):
print(i)
From the range() method documentation we can read the following:
The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method).
You also didn't specify the step you want to have. Because there are infinitely many real numbers between any 2 unique real numbers, you can't really get all of them.
What you can do is multiply both sides of the range by the same number until they are both integers and then when doing stuff with them divide them by that number again. I'm going to assume you wanted to have a step of 0.00001
mult = 100000
for i in range(1, 10000):
print(i/mult)
If you want any different step, simply supply a third argument to the range() method.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
I wish to accept a number in float from the user and give back an answer multiplying that number with pi value = 3.14
heres my code:
print "Enter a no."
n = raw_input('enter here: ')
result = 1
def gen_pi(x):
result = x*3.14
return float(result)
gen_pi(n)
its giving me an error of not being able to multiply non int sequence with float. what does it mean? anything wrong with the code?
The result of raw_input is a str which means that n is a str and then in your function, x is a str which can't multiply a float.
you'll want to convert x into a float before multiplying by pi.
There are lots of places to do this, but I'd recommend doing it before passing to gen_pi:
gen_pi(float(n))
This way, gen_pi can always assume it's working with a float and that code can be simplified:
def gen_pi(x):
return x * 3.14
Also note that if you want more precision (and possibly a bit more code clarity), you can use math.pi:
import math
def gen_pi(x):
return x * math.pi
This question already has answers here:
Can't convert string number value to float
(3 answers)
Closed 7 years ago.
I seem to have a problem with my code for a simple calculator with equations already codded into it that rounds to decimal points. The problem i have is that I have a variable that has a number from an equation but it isn't rounding to the nearest 2 decimal points when it needs to. Here is an example code:
def main():
variable1 = input("Input number")
variable2 = input("Input number")
V1 = float(variable1)
V2 = float(variable2)
variablesq = V1*V1
equation = 20242*(V2/variablesq)
answer = equation
round(answer, 2) #This is where the problem is occurring
print Answer
I do believe my mistake is that I'm using a variable instead of a set number but say the answer is 15.2353 it won't round the number from the variable to 15.24
Just calling the function won't do anything. You need to assign the result to something.
answer = round(answer, 2)
You can simply do "{0:.2f}".format(answer) that way you don't need to assign it to a variable.
Let's say there is a parameter n. Can n be any numbers? For example, question like this: Given a non-negative number num, return True if num is within 2 of a multiple of 10. This is what I am thinking:
def near_ten(num):
n = int #So I assume n can be any integer
if abs(num - n*10) <=2:
return True
Return False
However, there are two problems. First, in n*10, * is a unsupported operand type cuz I thought I could use Python as a calculator. 2nd, I cannot just simply say n = int, then n can be viewed as a variable as any number (or integer) in a math function. If there is a way that I could use n in that way, then life would be so much easier.
Finally I figure it out in another way which doesn't include "n" as a parameter:
def near_ten(num):
if num%10<=2:
return True
if (num+2)%10<=2:
return True
return False
However, I'm still curious about "n" as a parameter mentioned before. Since I'm just a starter, so this is really confusing.
In Python, int is a type. Types are first-class objects in Python, and can be bound to names. Of course, trying to multiply a type by a number is usually meaningless, so the operation is not defined by default. It can be referred to by the new name though.
n = int
print(n(3.4))
print(n('10') == 10)
Here is a much simpler solution:
def near_mult_ten(num):
return abs(num - (num+5) // 10 * 10) <= 2
Edit: Fixed.
try this:
d=a%10
if d<=2 or d>=8:
return True
return False
I am new to coding as well so forgive any mistakes.
i am not sure if you have ran your code or not, but python is a high level interpreted language, python CAN distinguish between variable types and therefore you do not need to explicitly declare them, your function header is valid.
you can also do operations between integers and floats/doubles without the need of casting, python already handles that for you
your function will raise an error in any compiler, ur n variable is declared, you have defined it, but you have not initialized it