Python Forum
My code is not printing, how do I get it to do so?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My code is not printing, how do I get it to do so?
#1
print('Enter number grade.')
grade = input()
if int(grade) < 60:
    print('F')
elif int(grade) == (60, 61, 62):
    print('D-')
The beginning to my code, it goes all the way up to the highest grade, my problem is, any number entered 60 or more, will simply not print the letter grade, where anything under 60 prints the F. Why are none of the other numbers printing their corresponding letter grade?
Reply
#2
Because you are checking if the integer value of your input grade is equal to (60, 61, 62)
which is a tuple and this condition can never be True so the print() function is never called
Reply
#3
look at line 5:
on right side you have tuple - (60, 61, 62)
on left side - int number - int(grade)
number and tuple can not be equal (i.e. that is comparing apples and bananas)
make line 5
elif int(grade) in (60, 61, 62):
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
You could check it this way:
elif int(grade) == 60 or int(grade) == 61 or int(grade) == 62:
which gets inconvenient if you want to check for even more values
so this might be one alternative
elif int(grade) in [60, 61, 62]:
of you check the range of the input:
elif int(grade) > 59 and int(grade) < 63:
Reply
#5
Is 'if' a requirement? for-loop seems more suitable for task at hand.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020