then later this is correct
if not (credits >= 120)and not (gpa >= 2.0):
but not this
if not credits >= 120and not gpa >= 2.0:
i understand that its most likely an order of operations syntax thing, but the latter should work even with the parenthesis because logically it should be able to interpret.
It’s mildly annoying that some questions are very US-centric. I had to look up what GPA is and whether 2.0 is good or bad before I could answer the question.
I’m new to coding so excuse me if this is a very basic question. I tried to go a step ahead of what was asked of me and though I accomplished what I wanted to, I would like to know why my two other attempts I tried previously didn’t work. I wanted a message to appear saying, “You can’t graduate” if someone didn’t meet the GPA or Credit requirements. My questions are, if the second if statement as shown below is indented, why will the code not work?
statement_one = (2 + 2 + 2 >= 6) and (-1 * -1 < 0)
statement_two = (4 * 2 <= 8) and (7 - 1 == 6)
credits = 110
gpa = 3.4
if credits >= 120 and gpa>=2.0:
print("You meet the requirements to graduate!")
if credits <120 or gpa<2.0:
print("You can't graduate")
My second question is when I tried to write the code as an else function, I also got a syntax error. Why is that?
statement_one = (2 + 2 + 2 >= 6) and (-1 * -1 < 0)
statement_two = (4 * 2 <= 8) and (7 - 1 == 6)
credits = 110
gpa = 3.4
if credits >= 120 and gpa>=2.0:
print("You meet the requirements to graduate!")
else credits <120 or gpa<2.0:
print("You can't graduate")
I was finally able to get the code to work using the elif function as well as another if function without indentation though.
Why is that? Your answer(s) would be greatly appreciated. Thank you in advance
statement_one = (2 + 2 + 2 >= 6) and (-1 * -1 < 0)
statement_two = (4 * 2 <= 8) and (7 - 1 == 6)
credits = 110
gpa = 3.4
if credits >= 120 and gpa>=2.0:
print("You meet the requirements to graduate!")
elif credits <120 or gpa<2.0:
print("You can't graduate")
Since this question was from more than a few months ago I’m sure you’ve already figured out the issue ;p
But for the sake of not leaving a question unanswered in case anyone has a similar question.
The first piece of code doesn’t work because to reach the second ‘if’ statement it must pass the first ‘if’ statement. And since they directly oppose each other the second one will never work.
For the second piece of code ‘else’ cannot have conditions.
Think of it this way:
if = if this is true
elif = (else if) the prior statement(s) is/are false and this is true
else = (otherwise) everything prior is false
An ‘else’ statement can have nested ‘if’ and ‘elif’ statements but ‘else’ itself cannot be modified because by modifying it it becomes selective, and it is characterised by it’s lack of selection. ‘Else’ applies to everything that didn’t match/get used by the previous statements.
For your code instead of writing
elif credits < 120 or gpa < 2.0:
you could just use
else:
credits = 100
gpa = 3.4
if credits >= 120 and [gpa > 2.0 ]:
print("You meet the requirements to graduate!")
if credits < 120 and [gpa < 2.0]:
print("you do not meet the requirements")
i wrote this code for task 2. please point out the mistakes
The square brackets look a little out of place. Less than has higher precedence than AND so no brackets are needed. When we need brackets for grouping, we use parentheses.
if ground_price < (drone_price and ground_premium):
…print(“Your best option is Ground Shipping with a price of”, ground_price)
elif drone_price < (ground_price and ground_premium):
…print(“Your best option is Drone shipping with a price of”, drone_price)
elif ground_premium < (ground_price and drone_price):
…print(“Your best option is Ground premium with a price of”, ground_premium)
Terminal: Your best option is Ground premium with a price of 125
it gives me the right answer, also this too gives the right answer
if (ground_price < drone_price) and (ground_price < ground_premium):
…print(“Your best option is Ground Shipping with a price of”, ground_price)
elif (drone_price < ground_premium) and (drone_price < ground_price):
…print(“Your best option is Drone shipping with a price of”, drone_price)
elif (ground_premium < ground_price) and (ground_premium < drone_price):
…print(“Your best option is Ground premium with a price of”, ground_premium)
Your best option is Ground premium with a price of 125
BUT if I do this it gives the wrong answer
if ground_price < drone_price and ground_premium:
…print(“Your best option is Ground Shipping with a price of”, ground_price)
elif drone_price < ground_price and ground_premium:
…print(“Your best option is Drone shipping with a price of”, drone_price)
elif ground_premium < ground_price and drone_price:
…print(“Your best option is Ground premium with a price of”, ground_premium)
Terminal: Your best option is Ground premium with a price of 162.5
so I’m just wondering if someone can tell me the best solution for parentheses with and