0

How to numerically order USER given numbers?

I am creating a program that asks the user an infinite number of times(unless user enters -99) to enter a number, when they are finished entering numbers the program should sort these numbers from smallest to largest in a list... should I be using a "while" or "for" statement?

22nd Feb 2017, 4:30 AM
auri
auri - avatar
7 Réponses
+ 1
make a list to store the numbers. each time you loop and get a new number, add it to the list. then sort the list # declare your list mylist=[] # right after you get a new number, add it to #your list mylist.append(number) # sort your list mylist.sort()
22nd Feb 2017, 5:04 AM
LordHill
LordHill - avatar
+ 1
There are min (), max () functions for that. I suggest you to read more before solving these questions.
22nd Feb 2017, 5:15 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
while. if your loop runs until a condition is met (-99 is input) then use a while loop. when you have a set amount of runs thru a loop, them use a for. to loop thru a list and check each number for instance.
22nd Feb 2017, 4:43 AM
LordHill
LordHill - avatar
0
haha..I see you have problem in choosing loop. you can work with any loop. but you know ending point then choose for and if you have to run till any specific condition satify then use while. But dont think much about choosing loops. .pick any and start doing.
22nd Feb 2017, 4:46 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
I def have a problem! Haha ok I'm at this point: While number!=-99 number=float(input('enter a number:')) So it keeps asking the user to enter a number! Yay me *eye roll* how would i begin to write a code to sort all of these (using the simplest techniques in python since I'm not very far yet)?
22nd Feb 2017, 5:01 AM
auri
auri - avatar
0
yes that you have to do. store these values in list, you can use append method to insert at the end and when finish inserting . sort these values using sort() method.
22nd Feb 2017, 5:07 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
0
Ok! Thanks! ... one more question if I wanted to only display the smallest and largest numbers entered, what would that look like? Thanks again! So helpful!
22nd Feb 2017, 5:13 AM
auri
auri - avatar