Subtraction + Decision and Loop
This is a twist on the previous exercise that will help you review loops and decision structures. You will again ask the user to enter two numbers. However, you will ALWAYS subtract the smaller number from the larger number to ensure that you never get a negative number for an answer. You do this by checking the numbers and switching them if they are not in the right order (larger then smaller). All of this checking, switching, subtracting, and output of the answer should occur in a function.
((( THIS WAS THE PREVIOUS EXERCISE
Function Basics - Arguments and Parameters
This is another easy exercise to test your knowledge of the fundamentals. In main(), ask the user to enter two integers. Pass those two integers to a function that will subtract one number from another. This function must also output the answer to the user.
Output:
Enter two integers (separated by a space) and this program will subtract them: [user enters: 5 7]
5 - 7 = -2
Notes and Hints:
1) From now on, you must use a function prototype for all programs that use functions. Don't expect me to ask for it in each exercise. )))
Finally, ask the user if they would like to run the program again. By now, you should know exactly what type of loop to use.
Output:
Enter two integers (separated by a space) and this program will subtract the smaller from the larger: [user enters: 7 5]
7 - 5 = 2
Do you want to run this program again? [user enters: y]
Enter two integers (separated by a space) and this program will subtract the smaller from the larger: [user enters: 5 7]
7 - 5 = 2
Do you want to run this program again? [user enters: n]
Notes and Hints:
1) As always, make sure you accept an upper or lower case 'Y'

Respuesta :

Answer:

In Python:

def subsmall(num1,num2):

   if num1 > num2:

       return num1 - num2

   else:

       return num2 - num1

   

repeat = True

while(repeat):

   num = input("Enter two integers: ")

   nums = num.split(" ")

   print(subsmall(int(nums[0]),int(nums[1])))

   runagain = input("Run program again? ").lower()

   if  runagain == "y":

       repeat=True

   else:

       repeat = False

Explanation:

The function begins here

def subsmall(num1,num2):

This subtracts num2 from num1 if num2 is smaller

   if num1 > num2:

       return num1 - num2

If otherwise, subtract num1 from num2

   else:

       return num2 - num1

The main begins here

This initialiazes a boolean variable to true    

repeat = True

This loop is repeated until the boolean variable is false

while(repeat):

Prompt to enter two integers

   num = input("Enter two integers: ")

Split the string by space

   nums = num.split(" ")

This passes the two integers to the function and also prints the differences

   print(subsmall(int(nums[0]),int(nums[1])))

Prompt to run the program again

   runagain = input("Run program again? ").lower()

If input is Y or y, the loop repeats

   if  runagain == "y":

       repeat=True

The program ends if otherwise

   else:

       repeat = False

ACCESS MORE
EDU ACCESS
Universidad de Mexico