"Input a list of ints. Stop when a non-int is entered. Find max and min of the list."
This one was also fairly easy. Python has a built-in method for finding the max of a list. That seemed a bit too easy, so I took an extra few minutes to do it myself by saving the vals to a new array, in order of size.
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()
try:
varFinished = "false"
while varFinished != "true":
val = raw_input("Number: ")
try:
val = int(val)
varList.append(val)
except:
varFinished = "true"
print "You've entered: "
for item in varList:
print (item)
varFinished = "false"
while varFinished != "true":
varLength = len(varList)
varTemp = varList[0]
for item in varList:
if item > varTemp:
varTemp = item
print "Your max value is: ", varTemp
for item in varList:
if item < varTemp:
varTemp = item
print "Your min value is: ", varTemp
varFinished = "true"
except:
print "Done"
1 comment:
I have never had any experience setting up IT security. But I’m sure your post helped other people. Thanks for sharing your experience with python exercise.
Post a Comment