2

The task at hand is quite simple, make a short program that asks for a cpsc prerequisite (number 217,219, or 233) and a math prerequisite (217 or 251) and if you have one of those classes as a prerequisite then it should say prerequisites met, if not then prerequisites not met. My code goes as follows (dont laugh, literally the biggest python noob):

cpsc=input("Which cpsc course have you taken (only pick one): ")
math=input("which math course have you taken (only pick one): ")

if cpsc==(217 or 219 or 233) and math ==(217 or 251)
    print("prerequisites met")
else:
    print("prerequisites not met")

Every input i have it only gives me the else print, i'm assuming its a problem with the comparisons in cpsc== and math==, what can i do to make this work? any and all help would be greatly appreciated.

icedwater
  • 4,701
  • 3
  • 35
  • 50

3 Answers3

5

Sorry but i laughed XD

Kidding, but.. Hahaha. Okay, sorry. Let's get to the point.

Why isn't it giving the desired output?

Your if statements have a little problem. Try testing this in your interpeter :)

a = 233
if a == (217 or 219 or 233):
    print (True)
else:
    print (False)

This will print False. Do you know why?

Actually, the use of or in your case, (217 or 219 or 233) is a little off. (You can try to evaluate (217 or 219 or 233)). Here, You're actually performing a logical comparison, using or as the operator (click the link, it's in C, but it should be easy to understand).

To solve this

You can use the in operator, make your 'desired' values a list:

if a in [217, 219, 233] and math in [217, 251]:
    #do something

Edit :)

Ah, yeah. I forgot to mention that the above code will not work just yet. You're missing one more thing.

What's that? It's the fact that input always gives you a string. So, if a has "217" as it's value, it still wouldn't work, because 217 (an integer) is not equal to "217" (a string).

To solve this, convert the input to integers with int() function.

cpsc = int(input("Which cpsc course have you taken (only pick one): "))
math = int(input("which math course have you taken (only pick one): "))

That's all.. Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
2

Welcome to StackOverflow and to Python! I'm going to correct a couple of mistakes and explain what you need to know.

cpsc=input("Which cpsc course have you taken (only pick one): ")
math=input("which math course have you taken (only pick one): ")

if (cpsc == 217 or cpsc == 219 or cpsc == 233) and (math == 217 or math == 251):
    print("prerequisites met")
else:
    print("prerequisites not met")

That should be working code. But why?

  • Python block statements (if, while, for, def, and so on) must be followed by a colon (:).
  • Blocks must be indented to work properly.
  • The or statement doesn't work like that. You need to check each condition individually, or do something like: if cpsc in [217, 219, 233].
Christian Ternus
  • 8,406
  • 24
  • 39
0

Your expressions ==(a or b or c ...) don't behave in the way you'r expecting. The expression a or b, ... undergoes what is called short circuit evaluation (you can read more about this). I.e:

>>> 1 or 2 or 3
1

Essentially each one resolves in this case to the first number in the expression: 217 and 217 in this case. You need:

if cpsc in [217, 219, 233] and math in [217 or 251]:
HennyH
  • 7,794
  • 2
  • 29
  • 39
  • Thanks for all the help! solved by putting in (217 or 219 or 233 in cpsc) and (217 or 251 in math): etc. etc. etc. im truly grateful for you assistance :D – Babi Rodriguez Malbo Oct 25 '13 at 04:08