Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code doesnt run
#1
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
Reply
#2
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
Reply
#3
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))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Thank you very much for the reply!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ldap3 Python print(conn.entries) doesnt work ilknurg 15 5,762 Dec-28-2022, 11:22 AM
Last Post: shad
  I have two Same Code but One of them Doesnt Work beginner721 6 3,090 Jan-22-2021, 10:56 PM
Last Post: beginner721
  code doesnt return anything ofrihemo 3 2,035 Jun-30-2020, 05:14 PM
Last Post: ofrihemo
  why doesnt the while loop run? supermane 3 3,101 Aug-04-2018, 06:01 AM
Last Post: wavic
  Why does this work and this doesnt= puruvaish24 1 2,599 May-22-2018, 03:58 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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