• Register
1 vote
936 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
2 answers 48 views
48 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 13 views
13 views
Problem: In a python program I am making, I want it to only take integers, and if it gets a string say "There has been an error in the system." instead of murmering sensless information the user will not understand
asked Jan 11 Mashhoodch 9.1k points
0 votes
1 answer 24 views
24 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 5.8k points
0 votes
1 answer 34 views
34 views
Problem: Hi there! I am a beginner, recently started learning Java, came across an objective question that was The "less than or equal to" comparison operator in java is used to _________. I left this blank as I was unable to recall what this operator does. Could someone please tell me the right answer? Also, give an example program that shows the functionality of this operator. Thanks!
asked Jan 17 Code Learner 5.8k points
0 votes
1 answer 1.6K views
1.6K views
Problem : I am finding solution on following issue deprecationwarning: elementwise == comparison failed; this will raise an error in the future.
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 19 views
19 views
Problem: Which of the following function convert a string to a float in python?Explain key differences between numbers and character strings. Use built-in functions to convert between integers, floating point numbers, and strings. Types control what operations (or methods) can be performed on a given value.
asked Feb 16 Mashhoodch 9.1k points
1 vote
1 answer 19 views
19 views
Problem: Hello all, I am a new python developer. I don&rsquo;t know if it would be fair to call me a developer because I&rsquo;ve just entered the programming world. I&rsquo;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&rsquo;d love to grab your tips. Thanks for your kind help.
asked Jun 28, 2020 adamSw 11.3k points
0 votes
1 answer 37 views
37 views
Explain with an example?
asked Oct 8, 2020 jagruthi 680 points
0 votes
1 answer 26 views
26 views
Problem: Python program to find second largest number in a listGiven a list of numbers, the task is to write a Python program to find the second largest number in given list. Examples: Input: list1 = [10, 20, 4] Output: 10 Input: list2 = [70, 11, 20, 4, 100] Output: 70 Method 1: Sorting is an easier but less optimal method. Given below is an O(n) algorithm to do the same. Python3 filter_none
asked Jan 23 Mashhoodch 9.1k points
0 votes
1 answer 6 views
6 views
Problem: Comparison Operators are used to perform comparisons. Concatenation Operators are used to combine strings. Logical Operators are used to perform logical operations and include AND, OR, or NOT. Boolean Operators include AND, OR, XOR, or NOT and can have one of two values, true or false. What is the name of the operator when applied to strings?
asked 3 days ago Ali 4.4k points