• Register
1 vote
1.3k views

Problem:

For the code below I can not get the correct answer:

square = 5

results = list(map(lambda a: 5 * a, range(square)))

for i in range(square):
 print("5 raised to the power of",i,"is",results[i])

 

This code gives the following output:

5 raised to the power of 0 is 0
5 raised to the power of 1 is 5
5 raised to the power of 2 is 10
5 raised to the power of 3 is 15
5 raised to the power of 4 is 20

 

The code is not giving me the actual result. If 5 raised to the power of 2 it should be 25 but the output is showing 10. 

What is the problem in my code can anyone sort out?

Thanks in advance.

8 6 2
4,980 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

In python to raise a number to the power of another number, you need to use the "**" operator. Where multiplying two numbers only uses one * symbol, the operator for raising one number to the power of another uses two: **.

What you did is you just multiplied the number thats why you are getting this type of results. So your code should be:

square = 5

results = list(map(lambda a: 5 ** a, range(square)))

for i in range(square):
 print("5 raised to the power of",i,"is",results[i])

And you should get the output:

5 raised to the power of 0 is 1
5 raised to the power of 1 is 5
5 raised to the power of 2 is 25
5 raised to the power of 3 is 125
5 raised to the power of 4 is 625

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 3 views
3 views
Problem : Need your help to solve this >'how to raise a number to a power in python' ?
asked Mar 17 sadi1982 36.3k points
0 votes
1 answer 15 views
15 views
Problem: Any ideas on what I need to do to fix this? Thanks!
asked Mar 5 ummesalma 29.2k points
1 vote
1 answer 38 views
38 views
Problem: Hello all, I am a new python developer. I don’t know if it would be fair to call me a developer because I’ve just entered the programming world. I’ve joined an online course. Besides that, I am having YouTube lessons and many online guides to ... this. Is there any better option that you can introduce to me? I’d love to grab your tips. Thanks for your kind help.
asked Jun 28, 2020 adamSw 11.3k points
1 vote
1 answer 12 views
12 views
Problem: Any ideas on what I need to do to fix this? Thanks! which operator is used by java runtime implementation to free the memory of an object
asked Mar 10 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 95 views
95 views
Problem: Hello! I tried running following program: class Program { static void Main(string[] args) { int x = 9; while (x++ < 10) Console.WriteLine(x); Console.ReadKey(); } } The output that I assumed was nothing but a blank screen because x++ = 9 ... 10 I am unable to understand the output. I want to know how that output was generated and in what manner the conditions and operators are evaluated.
asked Nov 28, 2020 Code Learner 9.9k points
0 votes
1 answer 159 views
159 views
Problem: Is there any way to retrieve a column that matches a certain pattern in SQL? If yes, how can it be done? Explanation with an example would be much appreciated. Thanks!
asked Mar 5 Code Learner 9.9k points
0 votes
1 answer 36 views
36 views
Problem: I have a database and I want to retrieve the records that come under a range of values. How can I do that? Is there any mechanism in SQL to do such a thing? If yes, please explain with examples because I am a beginner. Thanks!
asked Mar 5 Code Learner 9.9k points
0 votes
1 answer 7 views
7 views
Problem: how to Solve this: which of the following libraries is used in python to convert speech to text? HELP! I need help understanding this: which of the following libraries is used in python to convert speech to text? Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 13 Mashhoodch 13k points
0 votes
2 answers 62 views
62 views
How do I get a size of a pictures sides with PIL or any other Python library?
asked Oct 26, 2020 psandprop 2.4k points
0 votes
1 answer 8 views
8 views
Question: Many languages have a power operator; why doesn't C++?
asked Apr 14 Atik03 26.5k points