In Python, if-else statements execute from top to bottom, and once a condition evaluates to true, the program stops checking further conditions; therefore, when implementing grade conversion functions, conditions must be ordered from highest to lowest (e.g., checking for 90+ first, then 80+, then 70+, then 60+, and finally using else for scores below 60) to ensure correct results.
Deep Dive
Voraussetzung
- Keine Daten verfügbar.
Nächste Schritte
- Keine Daten verfügbar.
Deep Dive
If-Else functions practice - PythonHinzugefügt:
[sighs] >> Hi guys. Today we'll be working on if else statements. So the task for today is to create a function get letter grade that asks a user for a score from between 0 and 100 and converts it into a letter grade where A is 90 and above, B is 80 to 89 and so on and so forth where all the inputs are floats.
So, the first thing to do of course is to define a function called get letter grade.
letter grade, which takes in a score. Which we can just call score.
So, a lot of beginners start by doing this where they ask if score is greater than or equal to 60. And if it is, they will return D.
But watch what happens. If I pass an 85 here for example, and I put 85 as the score, it'll check that 85 is indeed greater or equal to 60. So, it will immediately return D. But that's completely wrong since 85 should be a B.
And here's the key idea. Python actually reads code from top to bottom. The moment it finds a condition that's true, it runs it and it stops checking everything else. So, if you start from the bottom, higher grades will never get a chance to be checked. So, instead, we actually need to flip the order. And let's say put this as 90.
And let's put this as return A.
So, we start from the highest grade first.
Uh next thing we can do is uh check the next highest grade using elif. So, let's say if it's not 90, it'll check if it's greater or equal to 80, it will return B.
And we could check this actually for the remaining conditions from um C to D actually. However, for F we will have to type um an else statement cuz that is actually the last grade that can be achieved.
So, if score greater or equal to 60 return D.
Oops.
So, for F uh since it'll check if none of these conditions are true, what's the last thing it can be? So, let's say it's like a score of 59. It'll check if it's greater or equal to 90 and it's not and it's not greater or equal to 80, not greater or equal to 70, not greater or equal to 60. So, that's why we just have an else statement here and it will be returning F.
So, this is actually the final code and uh let's actually just uh walk through it with a B5. So, um let's first just create an input. So, let's say score is an input where we can enter a score.
Um and the grade is equal to um whatever the score inputted into the function is and we can just pro- go ahead and actually print that.
Okay, so it's going to ask us to enter a score.
Let's just check with 85.
And as it turns out, it ends up being B, which is indeed in between 80 and 89.
Ähnliche Videos
resume fixed instantly 😭 Comment “app”andI’ll sendyou the link #parakeetaipartnership #resumetips
Ritcareer
686 views•2026-05-31
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03
Trends
Why Batman Lets The Joker Live 🤨
zackdfilms
9222K views•2026-05-30
They're Complete Trash
penguinz0
558K views•2026-06-04
The Murder of Deputy Caleb Conley
MidwestSafety
810K views•2026-06-04
I Bought FAKE HopeScope Merch (and paid a subscriber to give it a makeover) | Hopeful Hauls
HangWithHopescope
158K views•2026-06-04











