0

I've got a challenge to write a code to find the maximum in a list of numbers with no usage of such functions as max(), but only using for and if statement.

Hello, guys! So, here is my code:

student_scores = input ("Input a list of scores the students have got till now:\n").split(", ")
print ("Okkay, here is your list of student scores: ", student_scores)

max_ = student_scores[0]
for score in student_scores:
    if score > max_:
        max_ = score
print (max_)

but if I type such a list:

11, 44, 8, 234

the result is: 8

If I add

int() 

function to score and max_ variables, everything is correct. What's going wrong in that case? Help!

  • [How are strings compared?](https://stackoverflow.com/q/4806911) – 001 May 24 '23 at 13:52
  • 3
    "8" is indeed higher, in alphabetical order, than any of "11", "44", or "234". You aren't dealing with numbers here, these are all strings. – jasonharper May 24 '23 at 13:53

1 Answers1

0

You are using the string comparison.

student_scores = input ("Input a list of scores the students have got till now:\n").split(", ")

This line returns list of strings.

['11', '44', '8', '234']

Here is an answer about string comparison. How are strings compared?

and there is more better way to do this

student_scores = input("Input a list of scores the students have got till now:\n").split(",")

max_score = max((int(score)for score in student_scores))
fatihsucu
  • 144
  • 1
  • 9