So I was studying for an exam and i made a simple function to review 'sequential processing' and then I made this function(s) that turn a string input of whitespaced numbers into a list and scan to find the highest, but my conditional seems to think 8 is greater than 20 and 23. Can someone tell my why this is?
def findMax(numberlist):
maxval = numberlist[0]
for i in numberlist[1:]:
print("The value now is:\t",i,"\nThe max value is currently\t",maxval,"\n")
if(i > maxval):
maxval = i
print(maxval)
return maxval
def makeList(stringput):
print(stringput)
stringput = stringput.split(" ")
for i in stringput[0:]:
print("Value is:\t",i)
return stringput
findMax(makeList(input("\nWhat test are to be sorted\n")))
This returned the result 8 from the input "1 20 8 21"
Edit:Sorry, new user .
You're entering strings into python. That means that all sorting is happening alphabetically - not numerically.
For example, if you sorted the following two word alphabetically, which would you put first?
Apple
Bee
Apple right? Because A comes before B. Lets try again
8
23
It's going to be 23 - 8 is later in the 'alphabet'.
If you want to sort these properly, cast them all to ints first. Or use a library such as natsorted.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments