#๐Ÿ”’ helps

486 messages ยท Page 1 of 1 (latest)

bronze rose
#

send help

mellow domeBOT
#

@bronze rose

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.

bronze rose
#

i

#

@heady warren

gusty jungle
heady warren
#

hey

heady warren
bronze rose
#

i know thyem from the python discussion

gusty jungle
#

ah

#

my fault

heady warren
#

alright

golden moat
#

could you send your code?

bronze rose
#

#ValiderRejouer
def ValiderRejouer() -> bool:
    reponse: bool
    question:str 
    question = str(input("Voulez vous rejouer? (O ou N)\n"))
    if question == ("o"):
        reponse = True

    elif question == ("n"):
        reponse = False
        

    else:
        print(question)
    question:str 
    question = str("")

    print(question)
    return reponse
heady warren
#

lets go back to something imporant @bronze rose

#

and ill show you a few things

#

and ill show yoou more on how to make it into what you want

#

if that is ok

bronze rose
#

okay

#

@wispy grove

heady warren
#

type hints is a thing in python, and it has one purpose

#

name: str this is a type hint

bronze rose
#

okay

heady warren
#

and that point iis to tell the human what your intent is

#

i intent for the name name to be a string of type str

#

python does not use or care about this

#

it ignores it

#

type hints are for the human that reads your code

#

you can just remove it and it will not affect how python sees your code

#

because python just ignores it

#

you should use type hints, but know that python will not care about it at all

#

with me so far?

#

@bronze rose

bronze rose
#

sorry im back

bronze rose
heady warren
#

good!

#

lets not try to fix your code, but lets write it again how i would do it, then add the parts that you need for your school to be happy

bronze rose
#

i also want the function to work ๐Ÿ˜ญ

heady warren
#

you want the user to input o or n, and if the user does that, you are alright?

bronze rose
#

idk how to write it so it would work

heady warren
heady warren
bronze rose
#

no clue how to treat those

heady warren
bronze rose
#

it wouldnt work

#

bcz its uppercase

heady warren
#

im asking you the human,

#

not your computer program that does not work

#

i would argue that O and N are valid here

bronze rose
#

oh

#

yes its valid but python wouldnt agree

heady warren
#

what about oui and non ?

bronze rose
#

yes

#

but i only want it to be o or n

heady warren
#

so we have multiple things to look for, and that is something we can inspect later.. just have it in the back of the mind for now

#

let me write a simple while loop and we wiill use that as our starting point, and change iit over time

bronze rose
#

oaky

#

idk how im going to finish my homework in 5 days lmfao

heady warren
#
valid_input = 'o', 'n', 'O', 'N'
while True:
    user_input = input('write something')
    if user_input in valid_input:
        break
#

lets unpack this example

bronze rose
#

i thnk we need to use .uppercase or something

heady warren
#

explain it to me as you see it

heady warren
#

i know you said not to use break, and that is something to fix later, we are just looking at this as simple as possible.

bronze rose
#

valid input is o n O n

while the string is true (which is that smt is in it)
userinput is (something that the user write)
if o n O N is in valid input (which it always is)
it ends

heady warren
#

alright, fantastic

#

lets unpack a few moore thing

#

valid input is o n O n yes good.. this is correct

#

while the string is true (which is that smt is in it) im not checking a string

#

while True just means run forever

#

with me on this?

bronze rose
#

yes

#

i dont want to make an infinite loop tho

heady warren
bronze rose
#

okay sorry haha

heady warren
#

iif you start complex, your code will break

#

and you already know about that now..

#

๐Ÿ˜„

#

userinput is (something that the user write) yes correct and important

#

if o n O N is in valid input (which it always is) thats not what this if statement checks

#

can you see again?

bronze rose
#

hmmm

#

if the user input is in the choices of valid input

heady warren
#

correct

#

it might be or it might not be

#

if it is, we are ok, and we should end the loop

#

if it is not, we continue the loop

#

agree?

bronze rose
#

yes

heady warren
#

notice i have not put in annotations yet. because python does not use them, i will wait until the end to do so

#
valid_input = 'o', 'n', 'O', 'N'
while True:
    user_input = input('write something')
    if user_input in valid_input:
        break
#

are you allowed to use in ? like i used here?

bronze rose
#

nope

heady warren
#

how would you write that instead?

#

any suggestion?

bronze rose
#

oh i thought you meant if we were allowed to use in

#

um

#

it seems right

heady warren
#

yeah, thats what i asked aboout, schools have rules

bronze rose
#

i would write it

#

if user_input == valid_input

#

i have no clue sorry

heady warren
#

no worries

heady warren
#

what data type is user_input ?

bronze rose
#

str

heady warren
#

yes, and what about valid_input?

bronze rose
#

str too?

heady warren
#

no, its not, its a tuple

#

a tuple with four strings inside it

#

so to check for equality, you have to check each string

bronze rose
#

my brain is exploding

#

how is it a tuple if theres only text

heady warren
bronze rose
#

oh

heady warren
#

thats how you define a tuple

#

!e

my_thing = 1, 2, 3
print(type(my_thing))
mellow domeBOT
heady warren
#

so how can you write the check individual?

#

any ideas?

bronze rose
#

numpy

#

check each value

#

with i

heady warren
#

no, you normally just use in but to do it by hand you just check them one after each other

#

user_input == 'o'

#

user_input == 'n'

#

user_input == 'O'

#

user_input == 'N'

#

do you know how to add conditions together?

bronze rose
#

ohh

heady warren
#

when your writing a check?

heady warren
#

how?

bronze rose
#

if user input == o or user input ==......

heady warren
#

yes.. lets do that

#
valid_input = 'o', 'n', 'O', 'N'
while True:
    user_input = input('write something')
    if user_input == 'o' and user_input == 'O' and user_input == 'n' and user_input == 'N' :
        break
#

but can user input be both o and O at the same time?

bronze rose
#

cant you just do uppercase

heady warren
#

and tells you something needs to be both true

bronze rose
#

you cant put oOnN tho

heady warren
#

would it be better to write or ?

bronze rose
#

yes

#

or

heady warren
heady warren
#
while True:
    user_input = input('write something')
    if user_input == 'o' or user_input == 'O' oor user_input == 'n' or user_input == 'N' :
        break
bronze rose
#

sorry its just its 1:30am im just tryna skip a few steps so i cna maybe do another one because i have class is 7h lol

heady warren
#

now we are getting somewhere

bronze rose
#

thats why i said uppercase

heady warren
#

i is smart to use uppercase or lowercase, bacuse you half the amount of checks you have to write

#

how would you make the user_input upper or lower case?

bronze rose
#

i have no clue how to write that

#

user_input.lowercase

#

?

heady warren
#

yes, but you have to run it as well

#

!e

print('O'.lower())
mellow domeBOT
bronze rose
#

i think ill just do o or O or n or N lmao

#

it will be easier

heady warren
#
while True:
    user_input = input('write something')
    user_input = user_input.lower()
    if user_input == 'o' or user_input == 'n':
        break
bronze rose
#

oh wiat

heady warren
bronze rose
#

nvm that seems simpler

heady warren
#

yes, i agree

bronze rose
#

i was scared because of the tons of parenthesis lol

heady warren
bronze rose
#

the thing you wrote is really logical

heady warren
#

yes, because it is simple

#

now we only have to check lower case characters

bronze rose
#

you're a really good teacher lol

#

im uderstanding everything so far

heady warren
#

alright.. there is one final thing

#

it is how to swap out break with a condition (a rule for when the loop should end)

#

what is needed for this to work, have you been told?

bronze rose
#

a condition?

heady warren
#

yes, the rules of this condition

#

the thing you are checking in your condition, needs to change inside the loop

bronze rose
#

also for the function i need the function to return true or false and make it ask the questoin while the condition isnt o or n

heady warren
#

so now we will go from simple to a bit more comlicated

#

and this is something your teacher do on purpose

#

this is not something you would ever write for real

#

you want code to be simple

#

so lets add a running_loop condition instead of while True

bronze rose
#

i feel bad for taking so much for a single function when i have this to do

heady warren
#
running_loop = True
while running_loop:
    user_input = input('write something')
    user_input = user_input.lower()
    if user_input == 'o' or user_input == 'n':
        running_loop = False
#

take a look at this change

bronze rose
#

ohhhh

#

thats like a break

#

without a break

heady warren
#

notice it is far less simple, but it is similiar to the one i wrote

heady warren
#

and you need a variable value on the outside

bronze rose
#

ohhhhhhhhhhhhhhhhhhhhhh

heady warren
#

you need to check this in the loop as your conditiion

#

and you have to change that value inside the loop

#

so much work

#

three times the work, when you could just use a break

#

but do you agree that this example is the same?

bronze rose
#

yeah

bronze rose
#

wait nvm you cnat underrstand french lmfao

#

mb

heady warren
bronze rose
#

the cool thing is i understand what you're writing lol

heady warren
#

if you want to write this into a function, you have to have the function return something

#

what should a function of this do?

#

should it return True if o and False if n and continue the loop if anything else?

bronze rose
#

uhh

#

if its o you replay the hangman game

#

and if its n you just say thanks for playing or smt

heady warren
#

a function works well if it only does one thing

bronze rose
#

oh

#

if function true then replay

#

if function false print thanks for playting

heady warren
#

the if statement that sets the running variable to False, at that place in yoour code, your response is valid. agree?

bronze rose
#

uh

heady warren
#
running_loop = True
while running_loop:
    user_input = input('write something')
    user_input = user_input.lower()
    if user_input == 'o':
        running_loop = False
    elif user_input == 'n':
        running_loop = False
    else:
        print('invalid input')
#

i have made it less simple now

bronze rose
#

i still understand it tho

heady warren
#

yes

#

but because we started siimple

bronze rose
#

while the running loop is true check what user input said and if its o or n stop the loop if its smt else print invalid input

bronze rose
heady warren
#

this can be put into a function, because it has clear checks for what happends insiidde the loop

#
def yes_or_no():
    running_loop = True
    while running_loop:
        user_input = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
            return True
        elif user_input == 'n':
            return False
        else:
            print('invalid input')
#

a function will end when you use return

bronze rose
#
boucle: bool
def ValiderRejouer() -> bool:
    while boucle == True:
        input_utilisateur: str = str("Voulez vous rejouer?\n")
        input_utilisateur = input_utilisateur.lower()
        if input_utilisateur == ("o") or input_utilisateur == ("n"):
            boucle == True
        else:
            print(input_utilisateur)
#

here what i did

heady warren
#

and a loop will also end

heady warren
#

you have to assign it a value

bronze rose
#

can i just assign it to true or false

heady warren
#

you have to

#

so yes

#

look at my running_loop example

bronze rose
#

ohhh

heady warren
#

do you mean input here?

bronze rose
#

im not sure what to writre with the return tho

bronze rose
heady warren
#

write with the return?

bronze rose
#

i wouldve never noticed it lmao

bronze rose
#

i need to return if its true or not

heady warren
#

a function needs to return a value yes

#

did you look at my example with the return?

bronze rose
#

i dont see it

heady warren
#
def yes_or_no():
    running_loop = True
    while running_loop:
        user_input = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
            return True
        elif user_input == 'n':
            return False
        else:
            print('invalid input')

heady warren
bronze rose
#

i meant i didnt see the return example

#

oh yeah i need to do 1 return per function

heady warren
#

what happends in my yes_or_no functioon?

#

im still using the same code as before, almost at least

bronze rose
#

it just exists

#

wait no

#

you added return

#

it returns true or false

heady warren
#

if the user enters o or O it will return True

bronze rose
#

if its else its false

#

yeah

heady warren
#

if the user enters n or N it will return False

bronze rose
#

yeah

heady warren
#

if the user enters anytrhing else, the loop will go again

bronze rose
#
def ValiderRejouer() -> bool:
    boucle: bool = True #Assigner une valeur pour que la fonction fonctionne
    while boucle == True:
        input_utilisateur: str = str(input("Voulez vous rejouer?\n"))
        input_utilisateur = input_utilisateur.lower()
        if input_utilisateur == ("o") or input_utilisateur == ("n"):
            boucle == True
        else:
            print(input_utilisateur)
            return boucle
heady warren
#

is that part clear?

bronze rose
#

i think so yeah

heady warren
# bronze rose i think so yeah

in the function example, i am no longer doing something, because i change it to a return instead. what is that change i made?

#

if you focus on this, you will get there in a few moments

heady warren
bronze rose
#

uh

heady warren
#

so please dont do that

sharp pelican
#

What does this function supposed to do? Asking user whether to continue playing?

bronze rose
heady warren
bronze rose
heady warren
sharp pelican
#

then you should return the result of user input, true: y, false: n, keep asking: other inputs.

heady warren
#
def yes_or_no():
    running_loop = True
    while running_loop:
        user_input = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
+            return True
-            running_loop = False
        elif user_input == 'n':
+            return False
-            running_loop = False
        else:
            print('invalid input')
sharp pelican
#

then handle your game logic in the main loop according to the return value of this function

heady warren
bronze rose
#

im starting the homework

heady warren
#

can you see the change?

bronze rose
heady warren
bronze rose
#

oh

heady warren
#

so i dont need to tell the loop to end

bronze rose
#

if it stops why do you need true or false

heady warren
#

the loop can just run forever unless the user inputs the correct value

heady warren
bronze rose
#

uh

heady warren
#
def yes_or_no():
    while True:
        user_input = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
            return True
        elif user_input == 'n':
            return False
        else:
            print('invalid input')
#

now the while loop is a bit simpler

bronze rose
#

if it returns true does it loop back to the while

heady warren
bronze rose
#

why do u have it as true or false if it will end the function anyway

#

(my bad)

heady warren
#

because you have to return one value, and this function checks if the user enters yes or no

#

true means it entered o for yes

#

false means he entered n for no

#

but i could have returned other things

#

the logic of the loop is still the same

bronze rose
#

how are you going to stop it from making an infinite loop then

heady warren
#

what is unclear?

bronze rose
#

i dont wanna make this difficuly im actually trying ๐Ÿ˜ญ

heady warren
#

the loop is inside a function, the function ends... hence the loop does not run anymore

bronze rose
#

oHHH

#

I GET IT

heady warren
#

appreacite you for telling me when you dont understand

bronze rose
#

it returns nothing so because it will always be true UNLESS theres a return

heady warren
#

because that is important information, ii want to make you undersand it

bronze rose
#

OAIWNFPAOGISDNEPOWIWQH#EN:SHOIN

heady warren
bronze rose
#

and at the end in the code i can just make if ValiderRejouter == True reloop the whole code and if false print thank you for playing

#

OHHHH

heady warren
#

because the only way to get out of the loop is with a valid return

heady warren
#

but you dont need == True

#

you just do if ValiderRejouter()

bronze rose
#

what

heady warren
#

if something == True is the exact same as if something

#

python sees no difference

bronze rose
#

ohh

#

im done with 1/30 of the work lmfao

#

um what

#

^
SyntaxError: invalid syntax

& C:/Users/jacin/AppData/Local/Programs/Python/Python313/python.exe "c:/Users/jacin/OneDrive - Cegep de Lanaudiere/Programmation/2025-01/tp2_git/tp2.py"
File "<stdin>", line 1

heady warren
#
def yes_or_no() -> bool:
    while True:
        user_input: str = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
            return True
        elif user_input == 'n':
            return False
        else:
            print('invalid input')

notice i added type hints

bronze rose
#

i think i had those

heady warren
bronze rose
#
def ValiderRejouer() -> bool:
    boucle: bool = True #Assigner une valeur pour que la fonction fonctionne
    while boucle == True:
        input_utilisateur: str = str(input("Voulez vous rejouer?\n"))
        input_utilisateur = input_utilisateur.lower()
        if input_utilisateur == ("o") or input_utilisateur == ("n"):
            boucle == True
        else:
            print(input_utilisateur)
            return boucle
bronze rose
#

nvm im stupid

heady warren
#

does boucle change here?

bronze rose
#

i forgot something

heady warren
#

while boucle == True is the same as while boucle

bronze rose
#

oh

#
def ValiderRejouer() -> bool:
    boucle: bool = True #Assigner une valeur pour que la fonction fonctionne
    while boucle == True:
        input_utilisateur: str = str(input("Voulez vous rejouer?\n"))
        input_utilisateur = input_utilisateur.lower()
        if input_utilisateur == ("o"):
            boucle == True
        elif input_utilisateur == ("n"):
            boucle = False
        else:
            print(input_utilisateur)
            return boucle
#

i forgot to unindent one time the return tho

#

i think

heady warren
#

dont mix changing boncle and return

#

notice i did not do that at all

#

if the user enters W your else will run and return True

bronze rose
#

i cant do multiple return in one single function

heady warren
#

you have broken the logic of the code by mixing multiple things

bronze rose
#

oh no

heady warren
#

what happends if the user enters W ?

bronze rose
#

it prints voulez vous rejouer

#

and wait for input again?

#

is my print not okay here

heady warren
#

no

#

if the user enters W
three things happends in your code

#

if input_utilisateur == ("o"):

is this true or false?

bronze rose
#

uh

#

oh

#

it keeps running

heady warren
#

it is False, so it goes to the next

#

elif input_utilisateur == ("n"):

#

is this true or false?

bronze rose
#

ohh

#

false

heady warren
#

it is false, so it contineis to the next

#

there are no more, and else runs when all above are False

bronze rose
#

how can i even fix it

#

my brain is fried

heady warren
#

by not using iit

bronze rose
#

i cant put multiple returns in a singel function

heady warren
#

ofc you can

#

you just use it

bronze rose
#

no liek

#

my teacher doesnt want me to do that

heady warren
#

or do you mean, as a limit of the task?

heady warren
bronze rose
#

sorry

heady warren
#
def yes_or_no():
    while True:
        user_input = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
            return True
        elif user_input == 'n':
            return False
        else:
            print('invalid input')

that means one thing, your while looop will go back to be more complicated

#

because your loop need to end and then you do the one return

bronze rose
#

thats why i tried smt with boucle

heady warren
#
def yes_or_no():
    running = True
    while running:
        user_input = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
            running = False
        elif user_input == 'n':
            running = False
        else:
            print('invalid input')
    print(running) # what is the value here? 
heady warren
bronze rose
#

im gonna have a meltdown lmfao

heady warren
#

well, i added the extra print

#

what is the value on the last line?

bronze rose
#

is it gonna loop asking for the user for a value or not

#

true?

heady warren
#

if the user enters o here, what is the last print

bronze rose
#

false?

heady warren
#

yes

#

so the loop ends, you have a valid user input

#

if i to return True there, it means the loop ended, but might not be what you want

#

but once the loop has ended, you can return the one value you want.. what is that value your loooking for?

#

you can return user_input iit will be o or n

bronze rose
#

true or false?

heady warren
bronze rose
#

yes

heady warren
#

then lets add a new variable to keep track of if the answer is True or False

robust bison
#

hello

bronze rose
#

i think ill go to sleep really soon

#

i gotta be awake in 4h

robust bison
#

can i make with u some python projects

bronze rose
#

i will physically not be able to wake up tmr if we dont end this quick ahah

#

i love your help but i still wanna get some sleep x)

heady warren
#
def yes_or_no():
    running = True
    while running:
        user_input = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
            running = False
            response = True
        elif user_input == 'n':
            running = False
            response = False
        else:
            print('invalid input')
    return response
#

see?

bronze rose
#

OH

#

you used 2 comparisions

heady warren
#

it will return the response value only if the loop ends

bronze rose
#

one to keep the loop and one for the return

heady warren
bronze rose
#

you're so smart ๐Ÿ˜ญ

heady warren
heady warren
bronze rose
#

it kinda is

robust bison
heady warren
#

and code that is not simple, breaks really easily

heady warren
bronze rose
#

i think

#

ik theres 9000 ways to make this easier but this will do

heady warren
#

but now it does what you want

bronze rose
#

now im 1/35 of the way done

robust bison
bronze rose
#

and i have 5 days left

heady warren
bronze rose
#

idk what im gonna do lmao

#

yup

#

but teacher said no breaks

heady warren
robust bison
#

who can code using tkinter

heady warren
#

i think this is enough for today @bronze rose

bronze rose
#

yes

#

tysm

#

ill def ask for ur help tmr bcz im cooked if i dont understand

heady warren
#

notice that it is not easy to make something simple

bronze rose
#

idk what i learnt today

#

but i still understand what you wrote for me lmaao

bronze rose
heady warren
bronze rose
#

can i show u smt i did rq

heady warren
#

sure

bronze rose
#

its too long hold on

#

also dont mind the mistake

#

i edited it accidentraly

#

just assume everything works

heady warren
#

i can see it

bronze rose
#

#ValiderRejouer
def ValiderRejouer() -> bool:
    boucle: bool = True #Assigner une valeur pour que la boucle fonctionne
    reponse: bool = True # Pour que le jeu recommence ou non
    while boucle == True:
        input_utilisateur: str = str(input("Voulez vous rejouer?\n"))
        input_utilisateur = input_utilisateur.lower()
        if input_utilisateur == ("o"):
            reponse == True
            boucle == False
        elif input_utilisateur == ("n"):
            reponse == False
            boucle == False
        else:
            print(input_utilisateur)
            return reponse

#

is this better

heady warren
#

dont use return in the else

#
def yes_or_no():
    running = True
    while running:
        user_input = input('write something')
        user_input = user_input.lower()
        if user_input == 'o':
            running = False
            response = True
        elif user_input == 'n':
            running = False
            response = False
        else:
            print('invalid input')
    return response
#

notice that i have the return outside of the loop

bronze rose
#

ohh yeah

heady warren
#

the loop ends when you have valid user input

bronze rose
#
#ValiderRejouer
def ValiderRejouer() -> bool:
    boucle: bool = True #Assigner une valeur pour que la boucle fonctionne
    reponse: bool = True # Pour que le jeu recommence ou non
    while boucle == True:
        input_utilisateur: str = str(input("Voulez vous rejouer?\n"))
        input_utilisateur = input_utilisateur.lower()
        if input_utilisateur == ("o"):
            reponse == True
            boucle == False
        elif input_utilisateur == ("n"):
            reponse == False
            boucle == False
        else:
            print(input_utilisateur)
        
      return reponse
#

is that ok

heady warren
#

you should test it and see

bronze rose
#

File "<stdin>", line 1
& C:/Users/jacin/AppData/Local/Programs/Python/Python313/python.exe "c:/Users/jacin/OneDrive - Cegep de Lanaudiere/Programmation/2025-01/tp2_git/tp2.py"
^
SyntaxError: invalid syntax

dim knoll
#

Off topic but respect to both of yall for being dedicated for an hour and a half straight

bronze rose
#

because i have done 1/35 and i have 5 days left

dim knoll
#

You better start speedrunning

bronze rose
#

im reaching my limit

#

๐Ÿ˜ญ

dim knoll
#

Goodluck man

bronze rose
#

ill go sleep

#

im tired

#

im exhausted

#

my brain is dead

#

i wanna cry

#

thanks eivl ttyl ๐Ÿ˜ญ

#

(prolly tmr)

heady warren
#

ask me the next time you need something! ๐Ÿ˜„

#

good luck and sleep well

dim knoll
#

Sleep well mate

mellow domeBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.