Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 I am trying to swap two variables with a Function....
Obviously, I am a newbie and I have something wrong....! Could you help me see what my error is?



a = 100
b = 200

def swap(a, b):
    a = x
    b = y
    x = z
    b = x


swap (a, b)

print (a)  #Trying to get 200 

print (b)  #Trying to get 100

OK, so that code is wrong. I apologize for posting.

ADMIN, you can delete. I am not allowed to delete the original post.
ichabod801 wrote Mar-04-2019, 01:42 AM:
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.
You can't put a name on the right side of an assignment (=) until it's been on the left side of an assignment. Until it's on the left side, it doesn't exist. So x and y are going to cause name errors.

More generally, you need to store one of the values so you can access it after you have overwritten it with the other value.

I'm assuming this is homework, because there's an easier way to do this in Python.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

The parameters are exchanged within the function and do not affect the value of the variable with the same name outside the function.
a = 100
b = 200
def swap(a, b):
    # The above code is equivalent to  "a, b=b, a"
    return a,b

def swap2():
    global a,b
x,y = swap (a, b)
print (f"a={a},b={b},x={x},y={y}")
print (a,b)
a=100,b=200,x=200,y=100 200 100
The swapping with temporary variables is error prune.
Here an example with swapping:
def fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
And now the complicated version:
def fib(n):
    a, b = 0, 1
    for _ in range(n):
        x = a + b
        a = b
        b = x
Also a function should never change values outside of the function. This leads into an unexpected behavior. You can do this with short code.
In bigger projects, this is the opening of hell's gates. You deserve what you get.

In earlier programming languages there was a big difference between functions and procedures. A function is just something to do a calculation without affecting the outside world. A procedure was affecting the outside world, like printing on a terminal or printer.
snippsat likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Thanks, all.

This is good, learned a couple of things here....!


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  swap elements in list hshivaraj 3 309 Apr-22-2019, 09:23 AM
Last Post: Yoriz
  AssertionError: View function mapping is overwriting an existing endpoint function Zhavi221 7 1,164 Apr-17-2019, 01:07 PM
Last Post: Zhavi221
  I need help with loop one function and creating variables don 3 355 Jan-24-2019, 07:31 AM
Last Post: buran
  Function for grouping variables Scott 1 376 Nov-13-2018, 03:01 AM
Last Post: ichabod801
  Pass 2x Variables from Function to another Function MTom5 3 490 Aug-14-2018, 02:39 PM
Last Post: MTom5
  Run function in parallel but inherite dynamic data from the previous function atizva 4 749 Jul-11-2018, 06:39 AM
Last Post: volcano63
  replace-function for selveral variables.. Specdrum 2 523 Jul-04-2018, 10:41 AM
Last Post: Specdrum
  yes-no RE pattern swap bluefrog 1 662 Jun-08-2018, 07:06 AM
Last Post: volcano63
  When do you put variables inside vs outside a function? Drone4four 6 987 Jun-07-2018, 12:19 AM
Last Post: Drone4four
  4 byte hex byte swap from binary file medievil 7 5,542 May-08-2018, 08:16 AM
Last Post: killerrex

Forum Jump:

Users browsing this thread: 1 Guest(s)