"Accept a series of number inputs, and output them in order."
This exercise was relatively simple. Rather than use the built in string sort function, I decided to perform a simple sort by copying the max value per iteration to a new array. It's not the most efficient way of doing things, but it works.
Click to see Solution:
#!/usr/bin/env python
import sys
print "Input values until you are done. Enter any non-int character to stop adding values."
varList = list()
varNewList = list()
try:
varFinished = "false"
while varFinished != "true":
val = raw_input("Number: ")
try:
val = int(val)
varList.append(val)
except:
varFinished = "true"
varFinished = "false"
while varFinished != "true":
varTemp = varList[0]
for item in varList:
print "loop 2"
if item < varTemp:
varTemp = item
varNewList.append(varTemp)
varList.remove(varTemp)
print (len(varList))
print (len(varNewList))
if (len(varList))==0:
varFinished = "true"
print "the ordered #s: "
for item in varNewList:
print (item)
except:
print "Errored."
No comments:
Post a Comment