Need help with some simple code
I'm a new coder and I need help with some really basic codes but I genuinely can not wrap my head around this. I need the user to input the number of people participating in a race, how much time it took them to complete their race and then for the program to say 1) how much time did it take for 1st place to finish the race + his order and 2) how much time it took for 2nd place to finish + his order
(Yes I know I haven't done anything with sec and secOrder but that's because I don't know how to...)
68 Replies
think about it logically. When would it make sense to update the "second best" variables?
i thought about doing something like examining all the numbers and their orders (excluding the biggest one) but idk how to do that
or would it make sense to first update the 2nd one and only then seeing/updating the 1st place one?
Thats certainly one way to do it! You'd need a list, or an array. But you can do this without either of those.
lets walk through a potential scenario
The very first racer - whatever time they have will be the best time, right?
right
But what about the second racer? When will their time be the best?
they'll only be 1st place if sec > first
I meant second as in
i == 2
, not second as in second best 🙂their time will be the best if their time is smaller than first
okay, lets pretend it was
what must we do then?
then first = time and order = i
But that person now has the second best time
so that means that i need to move whoever was first to sec, right?
in that case, yes
essentially, for each time you inspect, there are 3 scenarios
1: it was the fastest time
2: it was the second fastest time
3: it was something else
we dont care at all about the third scenario here, so we can ignore it
can you write the code for detecting the first two scenarios?
remember, they cant both be true at once.
like this? but this doesnt feel right cause sec will get replaced with every new number that isnt the biggest one
nope, not correct
lets walk through it
the first if statement is correct, it checks if this is the new fastest time. The things you do IF it was true are wrong, however 🙂
but the second one is wrong, and its also running even if the first was true
so the 2nd if is supposed to be an 'else if' and the things that will happen if the first IF is right are supposed to be sec and secOrder?
else if
correct
and you need to update the second best before updating the new bestso then why wouldnt this work?
or even something like this
lets look at the first one
if the new time is the best time, update the second best time. stop.
result: best time is never updated, second best time is actually best time.
lets look at the second one
same problem and the second if also has issues
time > first
means "if its worse than the best one"
that will be true for everything but a new best timeso lets stick with the first one, how do i make it update the best time?
I think thats easy enough to figure out
but your second condition is still wrong
then what about this?
almost
but
time > sec
means "time HIGHER than sec"
thats hardly what we want, is it?is the problem the if statement? it gives me the same result whether its time<sec or time>sec
I mean, the actions you take in the ifs are also wrong
but for now I'm just trying to get you to fix your conditions
okay then if i switch it to time<sec , would that mean that both if condition are correct?
yes
Pobiega
1: it was the fastest time
2: it was the second fastest time
3: it was something else
Quoted by
<@105026391237480448> from #Need help with some simple code (click here)
React with ❌ to remove this embed.
we only care if the time is the new best time, or the new second best time
so i did this, which correctly gives me the 2nd best time, but the best time and its order are reset to 9999 and 0
can you, with words, describe what each condition is checking, and what actions it takes when that condition is true
the first condition checks the time and if its smaller than the 'first' number
if its true, then the best time is stored in sec and so is its i order
the 2nd condition checks if the number is smaller than 2nd place, in which case, if true, will replace the 'first number' with a number thats smaller than both the 'first' and 'sec' number ( not good )
no
First condition:
if the time is faster than the current best time... store that time as the second best time store that position as the second best orderthis is not correct if the time is the fastest, why are you saving it as the second best time?
so this?
but then the else if is never gonna happen
so it cant be right
so there needs to be something about both the best and 2nd best times in the first if condition, right?
yes!
it feels like the else if should be fine but something isnt right up top
you are so close
currently, you are updating both first and second´to the same value
thats not correct
if its the new fastest time, the current fastest time will be the new second fastest time
so the problem is somewhere up top?
yup
either in first=time or sec=time
Try to imagine this situation
you are standing at the finish line, and you are giving out medals as people come in
if their time is beaten, you take the medal and move it
the third person comes in and beats the new record
so you must give him the first place medal
what do you do with the silver medal?
the silver medal moves to whichever one was first before the 3rd person came
exactly
is your code currently doing that?
its giving gold AND silver to the 3rd person
so the problem is in sec=time
wait holy shit
i have cooked (?)
because firstly it needs to updat ethe 2nd one
go on..
you are so close
the times are fixed
but the orders are not
so just
switch the order and secOrder
but it gives me the same result
wait no
???
look at the values you are assigning
wait wdym order
as in order and secOrders
?
or orders of operation
the first one
but they look fine no?
no?
order = i
secOrder = i
you are giving them both the new valuebut what other value could i give them
are you serious?
yes, genuinely
secOrder = order;
before you update order
just like you did with the time, you need to do the same with the order
the position and time of the best/second best are two parts of one whole
if we modelled this better with an object instead of two separate variables, this would be more obviousso this?
pretty much
So uh...
This took about 20 times longer than I had expected it to, if I'm honest
what matters is the result :D
I get the feeling you are not really familiar or comfortable with logical thinking?
no not really
You will need to practice this, a lot.
i know, but thank you still