Python Forum
where to start with power program?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
where to start with power program?
#1
I have to write a program for my CS class and I have to make a program that will calculate the number to the x power all the way up to 8.

for instance if you input 3, it would output 3^2 is 9, 3^3 is 27 etc until the 8th power. I have no idea where to start with this. any input would be appreciated, thank you.
Reply
#2
You are going to need a for loop with the range function, and the power operator (**). If you can't get that to work, post your code in python tags, and tell us how it's not working.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
well first I put in

def pow (x, y)
and I just keep getting a syntax error

This is what it should be outputting but I don't know where to start



Please enter the base value: 3
3 to the 1 power is 3
3 to the 2 power is 9
3 to the 3 power is 27
3 to the 4 power is 81
3 to the 5 power is 243
3 to the 6 power is 729
3 to the 7 power is 2187
3 to the 8 power is 6561
Reply
#4
You need a colon at the end of a function definition.

I told you where to start. We don't write code for people around here, we help them fix their code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
When I enter this why doesnt python ask me for the "base value"?

def pow (x): 
  x = input ("Enter Base Value: ")
  input = str(x) **2 
  
 
  
  
Reply
#6
You have created a function, but you haven't actually run it.

after the function, not indented, add a line
pow()
also, there shouldn't be an argument in the definition of pow, or if there is,
there shouldn't be an input statement.
In the latter case, you would use, for example:
pow(5)
Reply
#7
You are overwriting the input function on the last line: input = str(x) **2
Doing this, input is no more the built-in function input(). So you try to use it in line 2 before initializing it on line 3. Change the variable name from input in line 3 to something else.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Oct-04-2017, 04:21 AM)wavic Wrote: You are overwriting the input function on the last line
Also overwriting the pow function for that matter.
Reply
#9
Yes pow() is built-in function. Didn't know it  Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
so I want to be able to vary the pow value since I want to get the power of any number from 1-8 what would I put in the pow ( )? I was thinking x or str(x) or just something that is variable
Reply


Forum Jump:

User Panel Messages

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