Question

Swapping Values in python Summary In this lab, you complete a Python program that swaps values...

Swapping Values in python

Summary

In this lab, you complete a Python program that swaps values stored in three variables and determines maximum and minimum values. The Python file provided for this lab contains the necessary input and output statements.

You want to end up with the smallest value stored in the variable named first and the largest value stored in the variable named third. You need to write the statements that compare the values and swap them if appropriate.

Comments included in the code tell you where to write your statements.

Instructions

  1. Write the statements that test the first two integers, and swap them if necessary.
  2. Write the statements that test the second and third integer, and swap them if necessary.
  3. Write the statements that test the first and second integers again, and swap them if necessary.
  4. Execute the program using the following sets of input values, and record the output:
    101 22 -23
    630 1500 9
    21 2 2

Grading

When you have completed your program, click the Submit button to record your score.

# Swap.py - This program determines the minimum and maximum of three values input by

# the user and performs necessary swaps.

# Input: Three int values.

# Output: The numbers in numerical order.

first = 0

second = 0

third = 0

# Get user input

first = int(input("Enter first number: "))

second = int(input("Enter second number: "))

third = int(input("Enter third number: "))

# Test to see if the first number is greater than the second number

# Test to see if the second number is greater than the third number

# Test to see if the first number is greater than the second number again

# Print numbers in numerical order

print("Smallest: " + str(first))

print("Next smallest: " + str(second))

print("Largest: " + str(third))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

Code:

# Swap.py - This program determines the minimum and maximum of three values input by

# the user and performs necessary swaps.

# Input: Three int values.

# Output: The numbers in numerical order.

first = 0

second = 0

third = 0

# Get user input

first = int(input("Enter first number: "))

second = int(input("Enter second number: "))

third = int(input("Enter third number: "))

# Test to see if the first number is greater than the second number

if first>second:

first, second = second, first

# Test to see if the second number is greater than the third number

if second>third:

second, third = third, second

# Test to see if the first number is greater than the second number again

if first>second:

first, second = second, first

# Print numbers in numerical order

print("Smallest: " + str(first))

print("Next smallest: " + str(second))

print("Largest: " + str(third))

Add a comment
Know the answer?
Add Answer to:
Swapping Values in python Summary In this lab, you complete a Python program that swaps values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Multiple Conditional Statements Summary In this lab, you complete a Python program that calculates an employee's...

    Multiple Conditional Statements Summary In this lab, you complete a Python program that calculates an employee's annual bonus. Input is an employee's first name, last name, salary, and numeric performance rating. If the rating is 1, 2, or 3, the bonus rate used is .25, .15, or .1 respectively. If the rating is 4 or higher, the rate is 0. The employee bonus is calculated by multiplying the bonus rate by the annual salary. Variables have been declared for you,...

  • unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that...

    unctions with No Parameters Summary In this lab, you complete a partially prewritten Python program that includes a function with no parameters. The program asks the user if they have preregistered for art show tickets. If the user has preregistered, the program should call a function named discount() that displays the message "You are preregistered and qualify for a 5% discount." If the user has not preregistered, the program should call a function named noDiscount() that displays the message "Sorry,...

  • In this lab, you add the input and output statements to a partially completed Python program....

    In this lab, you add the input and output statements to a partially completed Python program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you. Write input statements to...

  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • Hello! I'm looking for help with this assignment. Complete a program in pseudocode and Python that...

    Hello! I'm looking for help with this assignment. Complete a program in pseudocode and Python that performs the following tasks. Open the file called M4Lab1ii.py linked below these instructions in your M4 Content module in IDLE. Save as M4Lab1ii.py. Replace ii with your initials. [example for someone with the initials cc: M4Lab1cc.py] Complete Steps 1-7 as requested within the code’s comments. Run your program and test all four calculations, then exit the program. Save your program as M4Lab1ii.py using your...

  • Propose: In this lab, you will complete a Python program with one partner. This lab will...

    Propose: In this lab, you will complete a Python program with one partner. This lab will help you with the practice modularizing a Python program. Instructions (Ask for guidance if necessary): To speed up the process, follow these steps. Download the ###_###lastnamelab4.py onto your desktop (use your class codes for ### and your last name) Launch Pyscripter and open the .py file from within Pyscripter. The code is already included in a form without any functions. Look it over carefully....

  • Write a program in C that reads 20 float numbers from a file. The values are...

    Write a program in C that reads 20 float numbers from a file. The values are stored in a vector (1-dimensional array). Find and display the smallest number, the second smallest number, and the third smallest number of the set. Use only these libraries: stdio.h, stdlib.h, math.h, and string.h. Below is an example of the input file (in1.txt) and the resulting output file (output.txt). In1.txt: 3.14       4.05       -3.73      4.13       1.32 -2.21      0.46       4.57       4.64       -3.42 4.57       -0.14      3.00       -3.58      -0.78...

  • Two variables named largest and smallest are assigned for you. Use these variables to store the...

    Two variables named largest and smallest are assigned for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if statements, or elif statements as appropriate. There are comments in the code that tell you where you should write your statements. The output statements are written for you. Execute the program. Your...

  • Write a python program that repeatedly asks the user to input a pair of integers. The...

    Write a python program that repeatedly asks the user to input a pair of integers. The program should record the largest number of each pair into a list. The program should keep asking the user to input pairs of numbers until the user enters -1 for the first number. At this point the program should print the list on a single line, with each value separated by a space and then stop. Example of expected behaviour: Please enter first number:...

  • Can someone help me with this Python code Summary In this lab, you work with the...

    Can someone help me with this Python code Summary In this lab, you work with the same Python program you worked with in Labs 5-1 and 5-3. As in those earlier labs, the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. However, in this lab you should accomplish this using a while loop with a break statement. Instructions Make sure that the file NewestMultiply.py is selected and open. Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT