Reputation: 11
# Pseudocode
midterm <-- INPUT()
IF (midterm >= 90)
{
grade <-- "A"
}
IF ((midterm >= 80) AND (midterm < 90))
{
grade <-- "B"
}
ELSE
{
grade <-- "C"
}
DISPLAY (grade)
#Python
midterm = int(input("Enter your grade here")
if midterm >= 90:
grade = 'A'
if midterm >= 80 and midterm <90:
grade='b'
else:
grade='c'
print(grade)
Why is the output to this question letter grade C and not A? for all the values greater than 90 or lower than 80?
Upvotes: 0
Views: 77
Reputation: 140
As jasonharper mentions, the else is an alternative to the SECOND if statement you have written, NOT the first.
So if you input 95, the first if will set grade to "A", then the second if won't execute since the conditions are not met, but since the else statement is an alternative to the SECOND if statement and not the first, the else statement will set grade to "C" since the preluding if statement did not run.
The simple fix here is to change the SECOND if statement to an elif statement, so then the second if will become an alternative to the FIRST if and then the else will become the final alternative.
Upvotes: 1