-1

I'm a beginner at coding and use Python. I am trying to use an if else statement but the program only runs on the first statement I give it even if it doesn't meet the conditions to run. For example, if I put an answer other than "UG", "G", or "DL", the program still runs instead of stopping with an error message to the user. I use PyCharm btw.

Here is my project:

studentStatus = str(input("Are you an undergrad (UG), grad (G), or distance learner (DL) student?\n"))
if studentStatus == "UG" or "G" or "DL":
    homework = float(input("How many points did you get for your homework?\n"))
    homework = (homework / 800) * 100
    quiz = float(input("How many points did you get for your quizzes?\n"))
    quiz = (quiz / 400) * 100
    midterm = float(input("How many points did you get for your midterm exam?\n"))
    midterm = (midterm / 150) * 100
    final = float(input("How many points did you get for your final exam?\n"))
    final = (final / 200) * 100
    print(f"Homework: {homework:2.1f}%")
    print(f"Quiz: {quiz:2.1f}%")
    print(f"Midterm: {midterm:2.1f}%")
    print(f"Final: {final:2.1f}%")
else:
    print("Error. Student status must be UG, G, or DL.")

I couldn't find an answer on Google as to why my conditional statement isn't working, and I've tried switching the conditional statements so the error message is written first like this:

studentStatus = str(input("Are you an undergrad (UG), grad (G), or distance learner (DL) student?\n"))
if studentStatus != "UG" or "G" or "DL":
    print("Error. Student status must be UG, G, or DL.")
else:
    homework = float(input("How many points did you get for your homework?\n"))
    homework = (homework / 800) * 100
    quiz = float(input("How many points did you get for your quizzes?\n"))
    quiz = (quiz / 400) * 100
    midterm = float(input("How many points did you get for your midterm exam?\n"))
    midterm = (midterm / 150) * 100
    final = float(input("How many points did you get for your final exam?\n"))
    final = (final / 200) * 100
    print(f"Homework: {homework:2.1f}%")
    print(f"Quiz: {quiz:2.1f}%")
    print(f"Midterm: {midterm:2.1f}%")
    print(f"Final: {final:2.1f}%")

However, when I do this, the opposite issue happens and only the error message shows up even if I put the correct conditional statement. For example, when I put UG, G, or DL as an input it still read the error. I was expecting the program to skip over the if statement since the input doesn't meet the conditions I gave it.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
bubz
  • 1
  • 1
  • 2
    Does this answer your question? [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) – Friedrich Apr 05 '23 at 06:03

1 Answers1

0

it becomes

if (studentStatus == "UG") or ("G") or ("DL"):

"G" is a non empty string so this will always be true

if "G":
    print("UG is true...")

because of this you will ALWAYS enter this if statement

instead you can compare each to sudentStatus

if studentStatus == "UG" or studentStatus == "G"...:

or you can use list membership

# if its one of these things 
if studentStatus in ["UG","G","DL"]:

I am sure there is already a duplicate answer to this somewhere ... but not sure which and it wasnt in the reccommended questions

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179