Python Forum

Full Version: elif not responding on print
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
On the use of elif, the code is not responding to print
here is the code from Spyder (Python 3.11) IDE:

# -*- coding: utf-8 -*-
"""
Created on Thu Jul 20 16:39:11 2023

@author: hp
"""

grade = int(input("Enter the Grade: ")  )

if grade>90:
    print("You Got an AA! Congrats!")
    
elif grade>85 :
    print("You Got an BA! Congrats!")
  
elif grade>80 :
    print("You Got an BB! Congrats!")

elif grade>75 :
    print("You Got an CB! Congrats!")
0n the console response, It print the conditions IF, but not on the condition elif.

Python 3.11.3 | packaged by Anaconda, Inc. | (main, Apr 19 2023, 23:46:34) [MSC v.1916 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 8.12.0 -- An enhanced Interactive Python.

runfile('C:/Users/hp/Spyder/What is my Grade.py', wdir='C:/Users/hp/Spyder')
Enter the Grade: 90
You Got an BA! Congrats!

This version of python seems to be incorrectly compiled
(internal generated filenames are not absolute).
This may make the debugger miss breakpoints.
Related bug: http://bugs.python.org/issue1666807

runfile('C:/Users/hp/Spyder/What is my Grade.py', wdir='C:/Users/hp/Spyder')
Enter the Grade: 85

runfile('C:/Users/hp/Spyder/What is my Grade.py', wdir='C:/Users/hp/Spyder')
Enter the Grade: 80

runcell(0, 'C:/Users/hp/Spyder/What is my Grade.py')
Enter the Grade: 75

is it because of this comment on the console? How can this be corrected?
**This version of python seems to be incorrectly compiled
(internal generated filenames are not absolute).
This may make the debugger miss breakpoints.
Related bug: http://bugs.python.org/issue1666807

EddieG
The order was right, but your comparison is greater than, so the number must be bigger.
Changed the operator to greater equal than.

while True:
    grade = int(input("Enter the Grade: "))

    if grade >= 90:
        print("You Got an AA! Congrats!")
    elif grade >= 85:
        print("You Got an BA! Congrats!")
    elif grade >= 80:
        print("You Got an BB! Congrats!")
    elif grade >= 75:
        print("You Got an CB! Congrats!")
The while True loop is just for easier testing.
@DeaDEyE, the ">" explains why some grades are off, but does not explain why entering 85 or 80 does not appear to produce any output. I am stumped. When I run the program it produces output (the wrong output) when I enter 85 or 80. Or am I mistaken about what is meant by this in the OP:
Output:
runfile('C:/Users/hp/Spyder/What is my Grade.py', wdir='C:/Users/hp/Spyder') Enter the Grade: 85 runfile('C:/Users/hp/Spyder/What is my Grade.py', wdir='C:/Users/hp/Spyder') Enter the Grade: 80 runcell(0, 'C:/Users/hp/Spyder/What is my Grade.py') Enter the Grade: 75
Entering 85 should print "You Got an BB! Congrats!" and entering 80 should print "You Got an CB! Congrats!". These are both incorrect, but there should be a message printed.

From what I can find about the spyder warning, it appears to only affect debugging. Python code runs correctly. What happens if you run you code outside of spyder?
DeaD_EyE is dead on, as usual!

Try this maybe for clarity?

for grade in range(100, -10, -10):    
    if grade >= 100:
        print("You got an A+! Congrats!")
    elif grade >= 90:
        print("You got an A! Congrats!")
    elif grade >= 80:
        print("You got a B! Congrats!")
    elif grade >= 70:
        print("You got a C! Congrats!")    
    elif grade >= 60:
        print("You got a D! Congrats! Ever heard of the library?")
    elif grade >= 50:
        print("You got an E! Ever heard of studying?")
    elif grade >= 40:
        print("You got an F! Ever read a book?")
    elif grade >= 30:
        print("You got a G! Ever go to the class?")
    elif grade >= 20:
        print("You got an H! Hope your EQ is big!")
    elif grade >= 10:
        print("You got an I! Go back to kindergarten. Do not pass Go, do not collect £100.")
    else:
        print("You got NOTHING! Congrats! You won a pointy hat with a large D on it!")