![]() |
Code isnt working - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Code isnt working (/thread-13106.html) |
Code isnt working - abdullahali - Sep-28-2018 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? RE: Code isnt working - buran - Sep-28-2018 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... RE: Code isnt working - Skaperen - Sep-28-2018 what is your code doing wrong? is it printing out "42" or "foo"? RE: Code isnt working - ichabod801 - Sep-28-2018 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. RE: Code isnt working - gruntfutuk - Sep-29-2018 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)) RE: Code isnt working - Skaperen - Oct-01-2018 yeah, show your indentation. |