Python Forum
Python code doesnt run - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python code doesnt run (/thread-4663.html)



Python code doesnt run - tsetsko - Sep-01-2017

I have the following issue. I have this code:

def power(b, e):
result = b ** e
print " %d to the power of %d is %d." % (b,e,result)

power(37,2)
It is very simple one I know, but I am learning. I follow a course in codeacademy and this is one of the tasks. In codeacademy this code runs, with no errors. On my mac where I use Atom, the code runs as well. However on my windows (Visual Basic Code and Atom) it doesn't saying that there is an error on line 3 - point towards the end quote before the % (b,e,...

Does anyone know why the problem occurs?

I have python installed on both my Mac and PC, and I have included Python in the Path setting on my Windows machine.

Best Regards,

Tsvetan


RE: Python code doesnt run - Larz60+ - Sep-01-2017

your indentation is missing. Need 4 spaces for function body:
def power(b, e):
    result = b ** e
    print(" %d to the power of %d is %d." % (b,e,result))

power(37,2)
results:
Output:
 37 to the power of 2 is 1369.
A better way:
def power(b, e):
    result = b ** e
    # decimal output
    print("{} to the power of {} is {}".format(b, e, result))
    # hex output
    print("{} to the power of {} is {:02X} hex".format(b, e, result))

power(37,2)
results:
Output:
37 to the power of 2 is 1369 37 to the power of 2 is 559 hex



RE: Python code doesnt run - wavic - Sep-02-2017

It's beter in almost all cases for a function to return a something else than None. That way you could use the returned object for someting else.

def power(a,b):
    return a**b

print(power(37*3))
result = 40 + 49245 / 14 * power(3, 44)
print("{:.4}".format(result))



RE: Python code doesnt run - tsetsko - Sep-02-2017

Thank you very much for the reply!