Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
exec in a function
#1
Hi

I'm modifying a code by moving an "exec" inside a function creating an error; I wrote a small code highlighting what I'm facing.

What I'm missing and what is the correct programming ?

Thanks

What I'm missing

Sentence = "World is "

print("without using a function")
Add0 = 'beautifull'
exec("New0 = Sentence + '%s'" %Add0)
print("\t- Complete sentence 0 = {}".format(New0))

print("\nUsing a function")
def Complete(Part):
    Add = 'beautifull'
    exec("New = Part + '%s'" %Add)
    print("\t- Complete sentence 1 = {}".format(New))
    print("\t- Complete sentence 2 = %s" %(New))
    
Complete(Sentence)
Reply


Messages In This Thread
exec in a function - by paul18fr - Apr-18-2021, 07:33 PM
RE: exec in a function - by Yoriz - Apr-18-2021, 07:48 PM
RE: exec in a function - by paul18fr - Apr-18-2021, 07:59 PM
RE: exec in a function - by Yoriz - Apr-18-2021, 08:14 PM
RE: exec in a function - by paul18fr - Apr-19-2021, 07:11 AM
RE: exec in a function - by ndc85430 - Apr-19-2021, 07:59 AM
RE: exec in a function - by paul18fr - Apr-19-2021, 11:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to avoid exec(), globals(), locals(), eval() paul18fr 10 5,091 Apr-21-2021, 05:53 PM
Last Post: snippsat
  exec + subprocess = UnboundLocalError paul18fr 6 3,553 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  exec() in class, NameError niski1996 6 4,013 Apr-20-2020, 07:14 PM
Last Post: niski1996
  Is this use of exec pythonic? psolar 1 1,858 Feb-07-2020, 12:23 PM
Last Post: buran
  problem using exec to save local variables dkarl 0 1,812 Dec-01-2019, 08:52 AM
Last Post: dkarl
  common code, def a function vs exec() a string Skaperen 7 3,402 May-27-2019, 10:13 AM
Last Post: heiner55
  using function through exec() kiyoshi7 9 5,696 Mar-03-2018, 09:25 PM
Last Post: Larz60+
  namespaces in comprehension in exec() Skaperen 4 4,746 Mar-28-2017, 03:31 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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