#๐ helps
486 messages ยท Page 1 of 1 (latest)
@bronze rose
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 read this embed please
hey
its for me
i know thyem from the python discussion
alright
could you send your code?
#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
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
type hints is a thing in python, and it has one purpose
name: str this is a type hint
okay
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
sorry im back
yup i understand
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
i also want the function to work ๐ญ
you want the user to input o or n, and if the user does that, you are alright?
idk how to write it so it would work
we will get there in smal;l steps
yes
what if you write O or N ?
no clue how to treat those
im asking if that is valid or not
im asking you the human,
not your computer program that does not work
i would argue that O and N are valid here
what about oui and non ?
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
valid_input = 'o', 'n', 'O', 'N'
while True:
user_input = input('write something')
if user_input in valid_input:
break
lets unpack this example
i thnk we need to use .uppercase or something
explain it to me as you see it
we will get there, dont rush it
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.
i didnt see in so i cant use it either
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
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?
i said we will fix it later, we have to start simple
okay sorry haha
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?
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?
yes
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?
nope
yeah, thats what i asked aboout, schools have rules
no worries
you can only check equality for things that are equal
what data type is user_input ?
str
yes, and what about valid_input?
str too?
no, its not, its a tuple
a tuple with four strings inside it
so to check for equality, you have to check each string
the comma makes it a tuple
oh
:white_check_mark: Your 3.14 eval job has completed with return code 0.
<class 'tuple'>
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?
ohh
when your writing a check?
yes
how?
if user input == o or user input ==......
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?
cant you just do uppercase
lets make it simple first, then add more things
and tells you something needs to be both true
you cant put oOnN tho
would it be better to write or ?
if you use in you can. thats what in does, and you said you could not use it
good.
while True:
user_input = input('write something')
if user_input == 'o' or user_input == 'O' oor user_input == 'n' or user_input == 'N' :
break
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
now we are getting somewhere
thats why i said uppercase
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?
:white_check_mark: Your 3.14 eval job has completed with return code 0.
o
while True:
user_input = input('write something')
user_input = user_input.lower()
if user_input == 'o' or user_input == 'n':
break
oh wiat
you can do one or the other
nvm that seems simpler
yes, i agree
i was scared because of the tons of parenthesis lol
() is needed to run something in python
the thing you wrote is really logical
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?
a condition?
yes, the rules of this condition
the thing you are checking in your condition, needs to change inside the loop
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
yeah
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
why tho
i feel bad for taking so much for a single function when i have this to do
because there are many languages out there, and not all have in to check for this
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
notice it is far less simple, but it is similiar to the one i wrote
ohhhhhhhhhhhhhhhhhhhhhh
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?
yeah
do you think i have enough time in 5 days to write this?
wait nvm you cnat underrstand french lmfao
mb
yes, if you focus on thiinking simple and not so complicated
the cool thing is i understand what you're writing lol
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?
uhh
if its o you replay the hangman game
and if its n you just say thanks for playing or smt
a function works well if it only does one thing
the if statement that sets the running variable to False, at that place in yoour code, your response is valid. agree?
uh
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
i still understand it tho
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
yeah
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
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
and a loop will also end
boucle has no value before the while loop
you have to assign it a value
can i just assign it to true or false
ohhh
im not sure what to writre with the return tho
yeah my bad
write with the return?
i wouldve never noticed it lmao
dont i need to return smt
i need to return if its true or not
i dont see it
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')
im doing thisi iin steps with you so you understand
what happends in my yes_or_no functioon?
im still using the same code as before, almost at least
if the user enters o or O it will return True
if the user enters n or N it will return False
yeah
if the user enters anytrhing else, the loop will go again
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
is that part clear?
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
your focusing on diifferent things now..
uh
so please dont do that
What does this function supposed to do? Asking user whether to continue playing?
yes
its almost 2am sorry lol
im aware, but its only two more details to explain now ๐
im not sure what changes...
lets loook
then you should return the result of user input, true: y, false: n, keep asking: other inputs.
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')
then handle your game logic in the main loop according to the return value of this function
thank you, but im explaining a while loop from the beginning due to some previous missunderstandings
i dont even have the main loop
im starting the homework
can you see the change?
you stopped the loop from looping?
return stops the function
oh
so i dont need to tell the loop to end
if it stops why do you need true or false
the loop can just run forever unless the user inputs the correct value
you dont when you use it in a function
uh
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
if it returns true does it loop back to the while
no, return ends the function
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
how are you going to stop it from making an infinite loop then
return
what is unclear?
i dont wanna make this difficuly im actually trying ๐ญ
the loop is inside a function, the function ends... hence the loop does not run anymore
your not difficult, im just not using the correct words
appreacite you for telling me when you dont understand
it returns nothing so because it will always be true UNLESS theres a return
because that is important information, ii want to make you undersand it
OAIWNFPAOGISDNEPOWIWQH#EN:SHOIN
if the function returns, you have valid input yes
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
because the only way to get out of the loop is with a valid return
yes
but you dont need == True
you just do if ValiderRejouter()
what
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
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
i think i had those
you have understood how a loop works
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
i already understood how a loop worked but not within a funtion lmao
nvm im stupid
does boucle change here?
i forgot something
while boucle == True is the same as while boucle
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
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
i cant do multiple return in one single function
you have broken the logic of the code by mixing multiple things
oh no
what happends if the user enters W ?
no
if the user enters W
three things happends in your code
if input_utilisateur == ("o"):
is this true or false?
it is False, so it goes to the next
elif input_utilisateur == ("n"):
is this true or false?
it is false, so it contineis to the next
there are no more, and else runs when all above are False
by not using iit
i cant put multiple returns in a singel function
or do you mean, as a limit of the task?
oh i see..
sorry
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
thats why i tried smt with boucle
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?
yes, and remember that if you keep things simple, its easy to understand.. now we are making it more complicated to adhere to a school rule
im gonna have a meltdown lmfao
the code is back to beeing the same as before i made it more simple
well, i added the extra print
what is the value on the last line?
if the user enters o here, what is the last print
false?
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
true or false?
true if o and false if n?
yes
then lets add a new variable to keep track of if the answer is True or False
hello
can i make with u some python projects
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)
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?
it will return the response value only if the loop ends
one to keep the loop and one for the return
two variables yes
you're so smart ๐ญ
yes
notice that the code is now not simple anymore
it kinda is
u can use while True and in the end of the input .lower()
and code that is not simple, breaks really easily
im not looking to make it more complicated or complex
my brain is having a headache
i think
ik theres 9000 ways to make this easier but this will do
but now it does what you want
now im 1/35 of the way done
but it still nice damn
and i have 5 days left
break would soolve this in three lines ๐
now that you have the ability to take user input, you should be able to do the same with other functions
who can code using tkinter
#โ๏ฝhow-to-get-help please loook for your own help channel! ๐ this is occupied
i think this is enough for today @bronze rose
notice that it is not easy to make something simple
i knew how to do it just not with a function
well.. you have not learned it yet, you have to do it yourself and repate to learn
can i show u smt i did rq
sure
its too long hold on
also dont mind the mistake
i edited it accidentraly
just assume everything works
i can see it
#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
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
ohh yeah
the loop ends when you have valid user input
#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
you should test it and see
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
Off topic but respect to both of yall for being dedicated for an hour and a half straight
i wanna cry btw
because i have done 1/35 and i have 5 days left
Oh god
You better start speedrunning
Goodluck man
ill go sleep
im tired
im exhausted
my brain is dead
i wanna cry
thanks eivl ttyl ๐ญ
(prolly tmr)
Sleep well mate
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.