harsh_chane
harsh_chane

Reputation: 53

Using loops to find the highest score, lowest score from a list

Update: It seems that I understood the directions for the code wrong.

Absolutely new to Python, so any help is appreciated.

I am trying to find the highest score and the lowest score from an array using loops.

Here is the code I worked on:

nums = [6, 8, 9, 5, 3, 3, 5, 3]
highest_score = []
lowest_score = []

def calculate_Scores(nums):
  i = 1
  max = min = nums[0]
  if nums[0] > nums[1]:
    highest_score.append(nums[0])
  elif nums[0] < nums[1]:
    lowest_score.append(nums[0])
  while i < len(nums):
    if nums[i] > max:
      highest_score.append(nums[i])
    if nums[i] < min:
      lowest_score.append(nums[i])
    i += 1

 calculate_Scores(nums)
 print(highest_score)
 print(lowest_score)

Output:

 [8, 9] #highest
 [6, 5, 3, 3, 5, 3] #lowest

The code works just fine for the array given above, but when you change the array nums to:

[2, 8, 9, 5, 3, 3, 5, 3]

This is the output:

[8, 9, 5, 3, 3, 5, 3] #highest
[2] #lowest

How can I make it work so that 3 also goes to the variable lowest_score? Is there any other way I can make this whole process work? (Without using max() and min())

Upvotes: 2

Views: 5125

Answers (3)

paul lester
paul lester

Reputation: 1

Try this:

student_scores = input('Enter all scores at once, leaving a single space between each!').split()

for n in range(0, len(student_scores)):
    student_scores[n] = int(student_scores[n])
    
    
    highest_number = 0
    
    
for n in student_scores:
    if n > highest_number:
            highest_number = n
print(highest_number)  

Upvotes: -1

AllexPwns
AllexPwns

Reputation: 1

There is an easier way to do it. If you want to use a for loop:

nums = [6, 8, 9, 5, 3, 3, 5, 3]
highest_number = 0

for n in nums:
    if n > highest_score:
        highest_score = n
print(highest_number)

Or there is a built in function called max() which will do the same:

print(max(nums)) # outputs 9

Upvotes: 0

Green Cloak Guy
Green Cloak Guy

Reputation: 24681

If you're trying to find the highest number, why do you append to highest_score or lowest_score constantly? Wouldn't you want to only append the maximum or minimum? Why not calculate them first?

nums = [6, 8, 9, 5, 3, 3, 5, 3]
highest_score = []
lowest_score = []

def calculate_Scores(nums):
  i = 1
  max = min = nums[0]
  # first, calculate the max and min.
  while i < len(nums):  # (possible improvement: replace this with a for loop)
    if nums[i] > max:
      max = nums[i]    # replace max with the new max
    if nums[i] < min:
      min = nums[i]    # replace min with the new min
    i += 1
  # now that you've done that, add them to the highest_score and lowest_score
  highest_score.append(max)
  lowest_score.append(min)

Note that while this may be a useful educational exercise, there's no practical reason to not just use the built-in functions max() and min() in this situation (or really any other situation where you're trying to find the one element that out-competes others in some way).

Upvotes: 3

Related Questions