145k views
0 votes
Write a program whose input is two integers and whose output is the two integers swapped. Ex: If the input is: 3 8 the output is: 8 3 Your program must define and call the following function. swap_values() returns the two values in swapped order. def swap_values(user_val1, user_val2)

2 Answers

7 votes

Answer:

def swap_values(user_val1, user_val2):

return (user_val2, user_val1)

if __name__ == '__main__':

num1 = int(input())

num2 = int(input())

(num1,num2) = swap_values(num1,num2)

print(num1, num2)

Step-by-step explanation:

User Julio
by
4.5k points
4 votes

Answer:

The program to this question can be given as:

Program:

def swap_values(user_val1, user_val2): #define function

return (user_val2, user_val1) #return values

if __name__ == '__main__': #define constructor.

n1 = int(input('Enter first number :')) #input value form user

n2 = int(input('Enter second number :')) #input value form user

(n1,n2) = swap_values(n1,n2) #hold function values.

print(n1) #print values

print(n2) #print values

Output:

Enter first number :3

Enter second number :8

8

3

Explanation:

The explanation of the above python program can be given as:

  • In the python program we define a function that is "swap_values". This function takes two integer values that is "user_val1 and user_val2" as a parameters and returns variable values that is "user_val2 and user_val1".
  • Then we use a constructor in this we define two variable that is "n1 and n2" these variable are use to take user-input from the user and pass the value into the function.
  • To hold the value of the function we use n1 and n2 variable and print these variable value.
User Adamretter
by
5.4k points