#πŸ”’ The Winners and Their Scores Problem

69 messages Β· Page 1 of 1 (latest)

fringe jay
#

We have 2 inputs. Which is name[] and score[]. How do I make the output which is who the winner with the highest score is and what is the score? The output is like "The winner is Jonathan with score 86", Also, what if the scores owned by 2 or more people so the output is like "The winners are Jonathan, Chris, Melia with scores 86"

What's the problem here is i can sort the number for the biggest number. But I apparently can not connect the relation between name[] array and score[] array. Can anyone help me tho? πŸ₯²

ember ospreyBOT
#

@fringe jay

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fringe jay
#

@umbral robin

umbral robin
#

hi so

#

have you learned for loops yet?

fringe jay
#

Yeah

#

I learned while loop

#

That's why the teacher give me this homework

umbral robin
#

so you know how to use for loops?

#

also why do you need flush=True when you print?

fringe jay
#

For, do, i think it's for the next meeting tho. Today i learned about while only

umbral robin
#

ok

#

we can do this with while too

#

we should break down the problem into parts

#
  • get input from user about how many inputs we will receive
  • create lists
  • get data from user in a while loop, no need to have the x here, we can just use list.append()
  • get the item from the scores list which is the largest
  • get the name from the scores list with that same index
#

can you tackle these smaller problems?

fringe jay
fringe jay
umbral robin
#

ok, i could help you with one part

#
names = []
scores = []

idx = 0
while True:
  name = input("enter name >")
  score = float(input("enter score >"))

  names.append(name)
  scores.append(score)

print(names)
print(scores)
#

this is how you would get input, right?

fringe jay
#

Right

umbral robin
#

can you write a loop to find the largest element in an array?

#
x = [12, 5, 1, 5, 6, 0, 1, 200]

find the largest with a while loop
fringe jay
#

y = x[1]
m = 1
while m < 8
m = m+1
if y < x[m]
y = x[m]
print(y)

umbral robin
#

that is a good start!

#

however i have some tips for you!

#

the most important is: naming things is crucial in programming

#

second, you can use len(list) to get the length of it, so we dont overflow

#

third, you can use

#

!code

ember ospreyBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

umbral robin
#

also, indexing in most programming languages starts from 0, not 1

#
largest = x[0]

idx = 0
while idx < len(x):
  if x[idx] > largest:
    largest = x[idx]

  idx += 1

print(largest)
#

this is already good!!

#

its a major part done!

#

we need one more thing, tho

#

to keep track of the largest element, AND it's index!

#

!e

x = [12, 5, 1, 5, 6, 0, 1, 200]


largest = x[0]
largest_idx = -1

idx = 0
while idx < len(x):
  if x[idx] > largest:
    largest = x[idx]
    largest_idx = idx

  idx += 1

print(largest)
print(largest_idx)
ember ospreyBOT
#

@umbral robin :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 200
002 | 7
umbral robin
#

are you following so far?

fringe jay
#

Yeah, trying to understand all of this so far

umbral robin
#

ok!

#

!e once you understand it, take a look at this:

names = ["clein", "10x", "sani", "lemon"]
scores = [1, 10, 4, 7]

print('names', names)
print('scores', scores)
print('indexes', [0, 1, 2, 3])
ember ospreyBOT
#

@umbral robin :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | names ['clein', '10x', 'sani', 'lemon']
002 | scores [1, 10, 4, 7]
003 | indexes [0, 1, 2, 3]
umbral robin
#

largest score will be 10, largest index will be 1, so if you do names[largest_index] you will get the top one's name

fringe jay
#

Soo we use len command right?

umbral robin
#

yes!

fringe jay
#

Ahhh

umbral robin
#

!e

x = [1, 2, 3]
print(len(x))
ember ospreyBOT
#

@umbral robin :white_check_mark: Your 3.12 eval job has completed with return code 0.

3
fringe jay
#

I just found out that this command exists

umbral robin
#

yeap, its a built-in function, there are many

dense canyon
#

...are you in fact meant to write your own sorting algorithm, here? because the straightforward way of doing this is inds = sorted(range(len(names)), key=lambda i:scores[i]), though it takes using the key argument of sorted.

umbral robin
#

sorting?

#

wait

umbral robin
umbral robin
#

oh there can be multiple winners too

dense canyon
#

IMO, so is writing a sorting algorithm, so I don't really see what the intended solution here- ooh, I see

#

yeah, I totally missed that. only the indices of max scores is indeed much simpler than sorting them all.

fringe jay
#

Same scores but different name

gentle mural
gentle mural
dense canyon
#

the way i'd write a multiple-max is roughly (spoiler for people who want to figure out it themselves)
||

best_indices = []
best_score = -1 # some number that's <= than the highest score. could use scores[0]
i = 0
while i < len(scores): # in reality i'd use a `for i,el in enumerate(scores)` loop
    if scores[i] > best_score: # if we find a higher score, we replace our list of winners
        best_indices = [i]
    elif scores[i] == best_score: # if we find an equal score, we add to it intead
        best_indices.append(i)
    i += 1

||

ember ospreyBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.