3 Replies - 466 Views - Last Post: 27 October 2013 - 01:13 PM Rate Topic: -----

#1 ethopianprince  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-October 13

Complete noob here: need a specific problem solved.

Posted 24 October 2013 - 07:52 PM

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, im 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.

the else had a : too

This post has been edited by jon.kiparsky: 24 October 2013 - 09:09 PM
Reason for edit:: [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Complete noob here: need a specific problem solved.

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8037
  • View blog
  • Posts: 13,753
  • Joined: 19-March 11

Re: Complete noob here: need a specific problem solved.

Posted 24 October 2013 - 09:07 PM

Try evaluating the expression 217 or 219 or 233 in the interpreter.

English conjunctions are not like conjunctions in programming languages. For a start, work at arbitrary levels: the rule is (roughly) "if you can substitute P' for P, then you can also substitute "P' and P" for P, or you can substitute "P' or P". (notice also that "and" and "or" are almost synonymous in that sentence...)

This is NOT how they work in Python. Basically, you're hoping for some sort of distributive law:

if cpsc==(217 or 219 or 233) =>

if cpsc==217 or cpsc==219 or cpsc==233


Unfortunately, that's not how python works - or any programming language to my knowledge. You should review the syntax of the language, particularly the boolean operators.

In the meantime, pending that review, the expression you're looking for, for the first part of your expression, is the second one,
if cpsc==217 or cpsc==219 or cpsc==233


but a better construction in python is
if cpsc in [217,219, 233]


I'll let you sort out how to combine that with your second clause.

This post has been edited by jon.kiparsky: 24 October 2013 - 09:08 PM

Was This Post Helpful? 1
  • +
  • -

#3 ethopianprince  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-October 13

Re: Complete noob here: need a specific problem solved.

Posted 25 October 2013 - 11:49 AM

Haha, after much reviewing I found that cpsc in [the 3 variables] didnt work either, so after a bit of playing around i found that using
if [217,219,233] in cpsc and [217,251] in math 

worked! thanks for all your help and i greatly appreciate it, as i said before im a complete noob when it comes to this stuff and your help was greatly appreciated! (have the knowledge of the basics of python but still figuring out the details :P)

i mean from the mistake in the forum's gestures too you can see im a beginner :(

This post has been edited by andrewsw: 25 October 2013 - 12:07 PM
Reason for edit:: Tidied, removing unnecessary quotes

Was This Post Helpful? 0
  • +
  • -

#4 witeboy724  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 46
  • Joined: 21-June 12

Re: Complete noob here: need a specific problem solved.

Posted 27 October 2013 - 01:13 PM

View Postethopianprince, on 25 October 2013 - 11:49 AM, said:

Haha, after much reviewing I found that cpsc in [the 3 variables] didnt work either, so after a bit of playing around i found that using
if [217,219,233] in cpsc and [217,251] in math 

worked!

i mean from the mistake in the forum's gestures too you can see im a beginner :(/>/>



I feel like I should warn you that the way that was suggested to you was more close to correct even if it didn't work. When you take an input, it comes in as a string. So you should either convert that to an integer like this:
if int(cpsc) in [217,219,233]: # meaning if the integer of the typed string is in those list of 3 numbers then.. (will cause problems if input string isn't just numbers)


or

 if cpsc in ['217','219','233']:  # input is checked against string version of numbers rather than integers.  I prefer this in most cases


I know your way worked for you, but fyi, it is more pythonic to do it the other way. It should also help you avoid similar problems in the future. Happy coding
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1