#๐Ÿ”’ yo

213 messages ยท Page 1 of 1 (latest)

formal owlBOT
#

@solar tulip

Python help channel opened

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.

night vigil
#

you want to return True and return False

solar tulip
#

will attempt hold on

half pike
#

because you instantly return when you call the funciton

night vigil
#

your syntax is wrong

solar tulip
#

no worky

solar tulip
half pike
#
def grader(a, b, c):
    return
solar tulip
#

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

half pike
# solar tulip

you can delete everything after your first return it will never be reached

fleet canyon
#

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 ...)"

fleet canyon
# solar tulip

the moment Python sees the return statement it will return whatever is on that line and ignore the rest of the function

solar tulip
#

okay okay but what if I need multiple lines

#

yk like not everything can be fit into one line of code

half pike
#

just do not return in the first line ๐Ÿ˜‚

fleet canyon
#

do whichever logic those multiple lines entail first, and only then return their result

solar tulip
half pike
#

yes a return ends the function and jumps back to the caller

fleet canyon
#

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

solar tulip
#

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

fleet canyon
#

also, if ...: return True // else: return False is redundant. try to think a bit about how to simplify it ๐Ÿ˜›

solar tulip
#

typing that out makes sense but yk

#

I just assume I need to specify everything lmao

fleet canyon
#

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

solar tulip
#

oh okay I think I get what you're saying

#

okay we do be getting slightly somewhere

half pike
#

๐Ÿ’€

solar tulip
#

DOG

#

I blame the education system.

half pike
#
def grader(a,b,c):
    return True

this is what your function does

solar tulip
#

yeah

#

wait so itll always return true

#

aint no way

half pike
solar tulip
#

FUCK

#

oh it cut off

#

it says true

solar tulip
#

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

fleet canyon
# solar tulip https://gyazo.com/7af8d86d566896a5553d03215a54f659

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

solar tulip
#

so I need if statements?

half pike
#

no

solar tulip
#

):

fleet canyon
# solar tulip so I need if statements?

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

solar tulip
#

if this was Scratch it'd be all over rn. Js.

fleet canyon
solar tulip
#

I was joking I have never touched scratch in my entire life.

fleet canyon
#

I'm not even kidding, I really mean it.

solar tulip
#

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

half pike
#

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)

solar tulip
#

I did this. yk not to brag... kinda changing the game tho.

solar tulip
#

so I need uhhhh

half pike
solar tulip
#

thingys to use the info from a > b

#

but apparently that thingy is not an if statement

half pike
#

you want to combine them and use them for the return

solar tulip
#

okay how do you combine

fleet canyon
half pike
#

do you know logical junctors?

solar tulip
#

brother I dont know my gotdamn abc's okay

#

pulling up code academy rn.

half pike
solar tulip
#

YOU CAN USE THE WORD AND?

half pike
#

yes

#

it is a logical junctor

solar tulip
#

ok cool now I need to actually yk use the expressions results

half pike
#

fist combine the conditions to one using the andjunctor

solar tulip
#

good or nah

fleet canyon
#

pretty sure that'll give you a syntax error
(about indentation)

half pike
#

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?

solar tulip
#

I ran it, it didnt do anything

#

wait no I didnt

#

hold on

#

I forgot I changed it

#

okay it does

half pike
#

no need to run this

solar tulip
#

ran it

half pike
#

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

solar tulip
#

avg exams and avg hw both have to be greater than 75

#

can I not just combine the two

half pike
#

you did not code that

half pike
solar tulip
#

so I can or cant combine?

half pike
#

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

solar tulip
#

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

half pike
#

it is the same Redundancy we were trying to get rid of

solar tulip
#

does the redundancy actually create any issues besides being slower

half pike
#

insted of your code you could just do:

def grader(a,b,c):
    return c > 17
half pike
solar tulip
#

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

half pike
#

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

solar tulip
#

):

#

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

half pike
#

no

#

it does

solar tulip
#

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"

half pike
#

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

solar tulip
#

how in the fuck did it return 76

fleet canyon
#

what do you expect for python to do with ... and avg_exams or avg_hx > 80?

half pike
solar tulip
solar tulip
#

TEN MINUTES AGO I DIDNT KNOW YOU COULD USE THE WORD AND

#

Im wingin it ok

fleet canyon
formal owlBOT
fleet canyon
#

!or

formal owlBOT
#
The or-gotcha

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.")
half pike
# solar tulip how in the fuck did it return 76

!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

formal owlBOT
solar tulip
half pike
#

no

solar tulip
#

and itll check off each one

half pike
#

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__)

solar tulip
#

true

half pike
solar tulip
#

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"

half pike
#
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
solar tulip
#

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

half pike
#

no

solar tulip
#

and then say false if one wasnt and one was

#

so it checks them seperately?

half pike
solar tulip
#

ok I will attempt hold on

#

ok now none of them work

#

they all return false

#

I need true, false, false

half pike
solar tulip
#

fix?

half pike
#

yes (but i do not even know what your task is)

solar tulip
#

at this point my man

#

me neither

half pike
#

just send the task?

solar tulip
#

I think I did

half pike
#

no

solar tulip
#

#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

solar tulip
#

and not passing is returning false

half pike
#
attendance > 16 and avg_exams > 75 and avg_hw > 75 and (avg_exams > 80 or avg_hw > 80)
solar tulip
#

oh ok so I just had to put () around the 80 part

#

this feels like cheating ):

half pike
#

?

solar tulip
#

I was close tho

half pike
#

without the brackets it would be allways true when avg_hw is greater than 80

solar tulip
#

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

formal owlBOT
#
Python help channel closed

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