'Swapping variables in python with a function
I need to write a program whose input is two integers and whose output is the two integers swapped.
If input is: 3 8
, output is 8 3
.
The program must define and call the following function. swap_values()
returns the two values in swapped order.
def swap_values(user_val1, user_val2)
so far I have:
def swap_values(user_val1, user_val2):
return user_val2, user_val1
user_val1 = int(input())
user_val2 = int(input())
swap_values(user_val1, user_val2)
print (swap_values(user_val1, user_val2))
but that produces (8, 3)
and also doesn't work with a function call like swap_values(-1, 10)
.
So I'm wondering what I would need to do to get 8 3
for the input and then 10 -1
for the function call.
Solution 1:[1]
Placing the if statement to run as a script:
def swap_values(user_val1, user_val2):
swap = user_val1
user_val1 = user_val2
user_val2 = swap
return user_val1,user_val2
if __name__ == '__main__':
user_val1 = int(input())
user_val2 = int(input())
user_val1, user_val2 = swap_values(user_val1, user_val2)
print(user_val1, user_val2)
Solution 2:[2]
This is what I did for that same lab:
def swap_values(user_val1, user_val2):
swap = user_val1
user_val1 = user_val2
user_val2 = swap
return user_val1,user_val2
user_val1 = int(input())
user_val2 = int(input())
user_val1, user_val2 = swap_values(user_val1, user_val2)
print(user_val1, user_val2)
It worked fine in PyCharm and gave me the expected results, but zyBooks didn't like it and only gave be 2/10 credit. I reached out to the professor for help and they said I needed an if statement before the user_val inputs. I still can't figure out what the if statement should be though.
Solution 3:[3]
def swap_values(user_val1=None, user_val2=None):
swap = user_val1
user_val1 = user_val2
user_val2 = swap
return user_val1, user_val2
if __name__ == '__main__':
user_val1 = int(input())
user_val2 = int(input())
user_val1, user_val2 = swap_values(user_val1, user_val2)
print(user_val1, user_val2)
Solution 4:[4]
The answer to this question is very simple:
def swap_values(user_val1, user_val2):
t = user_val1
user_val1 = user_val2
user_val2 = t
return user_val1, user_val2
if __name__ == '__main__':
user_val1 = int(input())
user_val2 = int(input())
user_val1, user_val2 = swap_values(user_val1, user_val2)
print(user_val1, user_val2)
Solution 5:[5]
This question comes from a teaching website if anyone is wondering. As far as i know, this LAB is impossible. If you format the return the way it says, the interpreter responds that there are too many packets when it calls the function, but if you return userVal1, userVal2 then it is not formatted correctly. If you are given this assignment by your teacher, contact them, do not waste time trying to get full points. return either x or y in the following code to be correct for input tests (x) or for unit tests (y) pints though.
def SwapValues(userVal1, userVal2):
x = userVal2, userVal1
y = '{} {}'.format(userVal2, userVal1)
return x
if __name__ == '__main__':
print(SwapValues(input(), input()))
Solution 6:[6]
Actually, for the "function", all you really need is:
def swap_values(user_val1, user_val2):
return user_val2, user_val1
This effectively swaps the values on the return.
Solution 7:[7]
I completed the lab recently, this was my code.
def swap_values(user_val1, user_val2):
return user_val2, user_val1
if __name__ == '__main__':
user_val1 = (input())
user_val2 = (input())
swap_values(user_val1, user_val2)
print(user_val2, user_val1)
Solution 8:[8]
def swap_values(user_val1, user_val2):
return user_val2, user_val1
if __name__ == '__main__':
var1 = input()
var2 = input()
var1, var2 = swap_values(var1, var2)
print(var1, var2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Daniel Gardner |
Solution 2 | marcocunha |
Solution 3 | TMB |
Solution 4 | M.H. Tajaddini |
Solution 5 | Evan05 |
Solution 6 | Eric G. Moteberg |
Solution 7 | |
Solution 8 | Diedrate |