#๐ yo
213 messages ยท Page 1 of 1 (latest)
@solar tulip
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
you want to return True and return False
will attempt hold on
because you instantly return when you call the funciton
also that is not how you check multiple conditions
your syntax is wrong
yeah we havent even gone over this part in my class yet
def grader(a, b, c):
return
I was just having fun with my last homework and wanted to do some more lmao \
idk what im doing doe
oh yeah I didn't write the def part that was my professor
you can delete everything after your first return it will never be reached
python only sees a line with return and no values, so it returns None
you must specify the value you want to return in the same line as the return statement, as in return True or return False
you should format as "if ..., return True, otherwise return False" rather than "return if ... then ... otherwise ...)"
the moment Python sees the return statement it will return whatever is on that line and ignore the rest of the function
okay okay but what if I need multiple lines
yk like not everything can be fit into one line of code
just do not return in the first line ๐
do whichever logic those multiple lines entail first, and only then return their result
so my shit should be above return?
yes a return ends the function and jumps back to the caller
you can have multiple return statements inside of different if/elif/else branches, but it will stop the function as soon as it hits a branch with a return statement
BRO WHY DID MY FUCKIN PROFESSOR NEVER SAY THAT
oh my god
the last assignment I did would've been so much easier
ok thanks guys
also, if ...: return True // else: return False is redundant. try to think a bit about how to simplify it ๐
oh so itll just return false if its not true?
typing that out makes sense but yk
I just assume I need to specify everything lmao
if you don't explicitly return something, it will return None, but you could return an expression instead of just a literal
if you're checking if an expression has some exact value just to return that exact value, you could return the expression directly
just like how if condition == True: is redundant and equivalent to if condition: in most cases
๐
def grader(a,b,c):
return True
this is what your function does
correct
nvm :/
oh its printing True
it does genuinely nothing
how tf do you check values then man I dont get it
I need someone to explain this with like crayon and a napkin
remember, Python will execute operations sequentially, one line at a time, parsing the statements and expressions to determine what to do
things like a == b, x + 2, z > y are expressions and give you a value based on what you're telling python to do with these variables
things like variable = ..., if ...:, return xyz are statements and determine the flow of the program
You must provide values for the statements to operate on, and they will only care about what you explicitly give to them
in that image, you have a bunch of expressions doing comparison operations but you don't have any statements that use their results
so I need if statements?
no
):
You have to use the result of the expressions in some way
not necessarily if statements, just think a bit and maybe try to draw a flowchart indicating which operations you have to check, and how you have to use their outputs
if this was Scratch it'd be all over rn. Js.
Show how you would do it in Scratch
I was joking I have never touched scratch in my entire life.
I'm not even kidding, I really mean it.
only prior programming ive done is like
2 tutorial on c#
also I just wanna say yk I have had some wins ok hold on
simply writing an expression in the code and never using it is useless, the expression is evaluated and the result of the evaluation is simply deleted immediately by garbage collector if you do not use or save it, i.e. you can treat loose expressions as if they were not there, they do nothing (except wasting some processing time)
ok like how all of my a > b are doing nothing
so I need uhhhh
correct
thingys to use the info from a > b
but apparently that thingy is not an if statement
you want to combine them and use them for the return
okay how do you combine
||I was avoiding saying that as they can have side effects (e.g. list.append() or print())...|| but yeah you may as well think of it like that for now
do you know logical junctors?
ah that's right, I didn't think about side effects at that moment.
YOU CAN USE THE WORD AND?
ok cool now I need to actually yk use the expressions results
fist combine the conditions to one using the andjunctor
pretty sure that'll give you a syntax error
(about indentation)
and avg_exams will be evaluated to the truth value which the object itself has but i guess you want it to be greater than a number?
I ran it, it didnt do anything
wait no I didnt
hold on
I forgot I changed it
okay it does
no need to run this
ran it
your code is nonsense, now care about combine the conditon correct to one
your approach is wrong
you missed to do avg_exams > some value
than do that
you did not code that
no
so I can or cant combine?
you can combine logical expressions with logical junctors by the rules of boolean algebra not by things that may make sense to you in english
python is a programming language not english
ok I give up for now
ill prob know what to do when we actually go over it in class
thanks fellas
yoo it actually did something
we are so back
it is the same Redundancy we were trying to get rid of
does the redundancy actually create any issues besides being slower
insted of your code you could just do:
def grader(a,b,c):
return c > 17
why would you want to write bad code instead of good code
because I'd rather write bad code and then make it better than just not write any at all because I was too focused on making it the best to start with
optimized it doe
now I just have to somehow link 3 more variables together
you would only have to combine the conditions correctly and then return the combined condition. you almost had it. you would only have to correct the error in your boolean expression
just do a andbetween each condition
and than return the combined expression
):
sean my code fucking sucks dude
so Im assuming this is now ONLY caring about attendance and then returns True
so how tf do I make it also care about the other two
but it always returns false unless I change the attendance to a number < 17
or 17< ig
you get it
disregard that
can you use the word "or"
it does return true if the first variable is greater than 17 and the second variable is greater than 75 and the third variable is greater than 75 and only than in every other case it is false
maybe you should tell us what your task is when this is not what you want
what do you expect for python to do with ... and avg_exams or avg_hx > 80?
because that is the value of avg_exams and this is true and the end of a chained and
I feel like I just tried to solve 2+2 and got the answer blue
I DONT KNOW
TEN MINUTES AGO I DIDNT KNOW YOU COULD USE THE WORD AND
Im wingin it ok
!e print("red" or "blue" == "green")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
red
When checking if something is equal to one thing or another, you might think that this is possible:
# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
print("That's a weird favorite fruit to have.")
While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.
So, if you want to check if something is equal to one thing or another, there are two common ways:
# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
print("That's a weird favorite fruit to have.")
# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
print("That's a weird favorite fruit to have.")
!e
a = 5 and 7
print(a)
it is 7 because for an and every value needs to be true so it will be evaluated to the last one that is true or the first one that is false and both 5 and 7 are true because everthing except 0 is true so the expression is true and is evalutated to the last value 7. that is why you get 76 in your case
:white_check_mark: Your 3.12 eval job has completed with return code 0.
7
ok so all I have to do is a long list of "and" 's
no
and itll check off each one
you do not understand how and works and what a boolean value is and that there is a difference between the result of a comparison and the boolean value the object has itself (which comes from __bool__)
true
read this
this is moon runes
idk what any of this means
nvm I get it
ok hold on
Idk how to do "or" with numbers
what do I do instead of "favourite fruit"
a = 9
b = 7
a and b == 7
is the combination of two boolean expressions
exp1: a
exp2: b == 7
first they will be evaluated seperatly
exp1: a = 9 = True because every number that is not 0 has the boolean value True so it is evaluated to True
exp2: b == 7 = 7 == 7 = True
and now the values become combined to a new expression through the and junctor and we have:
exp3: True and True = True because and returns True if both expressions are True
see my issue is only one of the values (either hw or exams) has to be greater than 80
so if I did
hw and exams > 80
wouldn't it check to see if both were greater than 80
no
this checks what boolean value hw consists of and if exams is greater than 80
ok I will attempt hold on
ok now none of them work
they all return false
I need true, false, false
you did the same error again
yes (but i do not even know what your task is)
just send the task?
I think I did
no
yeah up here
#The attendance must be greater than 16 to pass.
#The avg_exams and the avg_hw must be greater than 75.
#Either avg_exams or avg_hw must be greater than 80 (or both of them).
returns should be
True
false
false
also "pass" in this case is just returning true
and not passing is returning false
attendance > 16 and avg_exams > 75 and avg_hw > 75 and (avg_exams > 80 or avg_hw > 80)
?
I was close tho
without the brackets it would be allways true when avg_hw is greater than 80
I just tried to rewrite it myself and it broke lmao
oh I forgot the and between 75 and (
ok I did it
thanks big dog @half pike
you're a real one
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
๐ yo