-5

So I got a homework assignment for my Python class that I simply cannot figure out how to do.

Essentially our professor wants us to break down the "sorted" built-in Python function and do it ourself while using else,elif,and if statements.

We are required to first ask the user to input 4 numbers and then sort and print them in ascending order using else,elif, and if while we are not allowed to use the built-in "sorted" function.

Here's what I need it to do:

Example output:

>>> sort4() 
Please enter a number: 1.1 
Please enter a number: -7.3 
Please enter a number: 32 
Please enter a number: 3.14 
Your numbers in ascending order are: -7.3 1.1 3.14 32

If there's anything that can be done to help or assist me, please do so ASAP. Thanks!

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
Robert Montz
  • 163
  • 1
  • 1
  • 9
  • see [this question](http://stackoverflow.com/questions/895371/bubble-sort-homework). – Andy Hayden Sep 26 '12 at 01:52
  • @hayden: It's not the same question. That one talks about bubble sort. The OP's homework question is trying to teach control flow (hence the forced `if`, `elif` and `else`). Further, `sorted` does not implement bubble sort. – inspectorG4dget Sep 26 '12 at 01:54

1 Answers1

2

First, write out your algorithm in pseudo-code (this is a very naive algorithm):

  1. get four numbers from the user
  2. take the first number (call it first)
  3. check if it's smaller than the second number (called second)
  4. if first is smaller than second check it against the other numbers (third and fourth)
  5. if after such checking, first is the smallest of the four numbers, print it out
  6. if first is bigger than any other number encountered, let that other number be called first instead, and check it against the other two numbers.

Once the first number has been printed out, repeat the process with the other three numbers

Now, try to code this up and we'll help if you stumble again

EDIT 1 ('how do I get it to do "and if"s'):

An "and if" is essentially two nested if statements. For example:

If I wanted to say (in pseudo code) "if it is raining and if I don't have the car, I will carry an umbrella", then I would say:

if it is raining
    if I do not have the car
        I will carry an umbrella

So, to do that in python, you will have to do:

if conditionA:
    if conditionB:
        # do stuff…

Hope that helps

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241