Python Forum
Help in exercise for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help in exercise for loop
#1
Hello!

I've just started a python course and I'm trying I do not know how to do this exercise, anyone can help me?

This is the exercise "Write a program using a for loop that calculates exponentials. Your program should ask the user for a base
base and an exponent exp, and calculate it"

Thanks in advance
Reply
#2
Perhaps time to re-read the lesson

hint: https://codereview.stackexchange.com/que...-in-python
Reply
#3
I've read the post that you've shared and I'm still don't know how to make it. The problem is that I do not know why I need to use the for loop if I just have the base and the exponent given the user responses. This is my query:
base = input("choose a power base: ")
exp = input("choose a power exponent: ")

result=base**exp
print result
Reply
#4
You don't need the for loop.
Thought that was part of the exercise.
Reply
#5
It's just an exercise to learn about using loops. I admit given the built-in tools to calculate powers it may look stupid, but nevertheless you have to do it if it is the requirement of your homework. Note that they want you to use for loop, not while loop.
Also, it's not clear how restrictive you should be about exp, e.g. do you accept negative numbers, do you accept 0, do you accept float? Or you accept just positive int as exp?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Exactly, my problem is that I don't see how to do it with the for loop .
For the restrictions as it doesn't say anything I was thinking on doing it with int and accepting negative numbers.
Reply
#7
What is x ** 2? It is x * x. What is x ** 3? It is x * x * x. What is x ** 4? It is x * x * x * x. Do you see how you would do it with a loop now?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
That's the best query I have figured out:
base = input("choose a power base: ")
exp = input("choose a power exponent: ")

for x in range(exp+1):
    x=base**x
print x
I haven't been able to do it with the x*x*x form, can you tell me if there is a better way to do it?
base = input("choose a power base: ")
exp = input("choose a power exponent: ")

for x in range(exp+1):
    x=base
    base*=base
print x
Thanks
Reply
#9
Your second one is close. You want to start with 1, just before the loop, in a variable named something besides x or base (try result). Then you want to multiply that variable by base each time through the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
Thanks, I've finally solved :)
base = input("choose a power base: ")
exp = input("choose a power exponent: ")
result=1
 
for x in range(exp+1):
    x=result
    result*=base
print x
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020