Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code isnt working
#1
Write the definition of a function powerTo which recieves two parameters, a double and an integer. If the second parameter is positive, the function returns the value of the first parameter raised to the power of the second. Otherwise, the function returns 0.

My code:

def powerTo(double, int):
if int>=0
return double**int
else:
return 0

any idea why its not working?
Reply
#2
Please, repost with code in python tags and preserved indentation. Also, if you get any errors, post the full traceback in error tags.
not working is not very descriptive what the problem is...
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
#3
what is your code doing wrong? is it printing out "42" or "foo"?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
And don't name things int. That is a built-in, a basic part of Python, which you have no access to after you use it as a variable name.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Assuming you have your indention correct (we can't tell, because you have not put BBCode tags around your code yet), then you are missing a : at the end of the if statement line. As mentioned, best not to use things Python already uses as your variable names.

Here's a corrected version of your code, and also an alternative approach, as well as example usage. Note I changed int to int_ to avoid hiding the int type.

def powerTo(double, int_):
    if int_ >= 0:
        return double ** int_
    else:
        return 0

def powerTo2(double, int_):
    return 0 if int_ < 0 else double ** int_

for num in range(-2, 4):
    print(powerTo(5, num), powerTo2(5, num))
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
yeah, show your indentation.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting my choices to display fro some reason it isnt working. dgizzly 5 1,337 Oct-24-2022, 04:53 PM
Last Post: DeaD_EyE
  Python code not working garvind25 1 2,724 Jun-24-2020, 09:38 PM
Last Post: Yoriz
  Please help me get this code working that i haven't posted yet damnit 3 2,412 May-22-2019, 09:57 PM
Last Post: Yoriz
  Homework Teacher isnt helping need some help randyearthchild 2 2,895 Nov-05-2017, 11:32 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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